微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在Flutter中使用Dart http包指向localhost:8000?

如何解决如何在Flutter中使用Dart http包指向localhost:8000?

简短答案:您可以传递Uri而不是字符串作为参数

      var client = createHttpClient();
      client.get(new Uri.http("locahost:8000", "/category"));

解决方法

我正在遵循Flutter Networking /
HTTP教程
对运行在localhost:8000上的服务器进行GET请求。通过浏览器访问我的本地主机可以正常工作。我的代码如下所示:

var url = 'http://localhost:8000';
Future<String> getUnits(String category) async {
    var response = await httpClient.get('$url/$category');
    return response.body;
}

当我指向任何真实的网址(例如)时,此方法工作正常https://example.com,但是当我指向https://localhost:8000https://localhost(或它们的任何变体)时,出现以下错误:

E/flutter ( 4879): [ERROR:topaz/lib/tonic/logging/dart_error.cc(16)] Unhandled exception:
E/flutter ( 4879): SocketException: OS Error: Connection refused,errno = 111,address = localhost,port = 47060
E/flutter ( 4879): #0      IOClient.send (package:http/src/io_client.dart:30:23)

每次我重新加载应用程序时,上述错误中的端口都会更改。我查看了http程序包代码,似乎没有一种方法可以指定URL的端口。我如何指向我的本地主机?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。