未定义createHttpClient-颤振

时间:2020-08-20 10:32:45

标签: http flutter

我想通过HTTP侦听器解析JSON。我是Flutter的新手,所以我在网络上对其进行了搜索,并向YAML文件中的依赖项添加了http: ^0.12.2程序包,但仍然收到错误消息:"The method 'createHttpClient' isn't defined for the type '_MyHomePageState'."

这是我的两个文件main.dartpubspec.yaml。在main.dart中,import 'package:flutter/material.dart';看起来像是未使用并显示为灰色。你能帮我我所缺少的吗?

main.dart:

import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:http/http.dart';

void main() {
  runApp(new MyApp());
}


class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key}) : super(key: key);

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String _ipAddress = 'Unknown';

  _getIPAddress() async {
    String url = 'https://httpbin.org/ip';
    var httpClient = createHttpClient();
    var response = await httpClient.read(url);
    Map data = json.decode(response);
    String ip = data['origin'];

    // If the widget was removed from the tree while the message was in flight,
    // we want to discard the reply rather than calling setState to update our
    // non-existent appearance.
    if (!mounted) return;

    setState(() {
      _ipAddress = ip;
    });
  }

  @override
  Widget build(BuildContext context) {
    var spacer = new SizedBox(height: 32.0);

    return new Scaffold(
      body: new Center(
        child: new Column(
          children: <Widget>[
            spacer,
            new Text('Your current IP address is:'),
            new Text('$_ipAddress.'),
            spacer,
            new RaisedButton(
              onPressed: _getIPAddress,
              child: new Text('Get IP address'),
            ),
          ],
        ),
      ),
    );
  }
}

pubspec.yaml

name: httpdeneme2
description: A new Flutter application.



environment:
  sdk: ">=2.7.0 <3.0.0"

dependencies:
  http: '^0.12.2'
  flutter:
    sdk: flutter


  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^0.1.3

dev_dependencies:
  flutter_test:
    sdk: flutter

2 个答案:

答案 0 :(得分:3)

您需要像这样导入http包

 import 'package:http/http.dart' as http;

然后用下面的代码替换您的方法

 _getIPAddress() async {
    String url = 'https://httpbin.org/ip';
    var response = await http.get(url);
    Map data = json.decode(response.body);
    String ip = data['origin'];
    

    if (!mounted) return;

     setState(() {
      _ipAddress = ip;
   });
}  

答案 1 :(得分:2)

您可以这样导入http包:

将“ package:http / http.dart”导入为http;

并在您的代码中,使用以下代码进行更新:

_getIPAddress() async {
    String url = 'https://httpbin.org/ip';
    var response = await http.read(url);
    Map data = json.decode(response);
    String ip = data['origin'];

    // If the widget was removed from the tree while the message was in flight,
    // we want to discard the reply rather than calling setState to update our
    // non-existent appearance.
    if (!mounted) return;

    setState(() {
      _ipAddress = ip;
    });
  }