相关疑难解决方法(0)

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

我正在按照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;
}
Run Code Online (Sandbox Code Playgroud)

当我指向任何真实的URL时,这可以正常工作,例如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)
Run Code Online (Sandbox Code Playgroud)

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

http localhost dart flutter

15
推荐指数
9
解决办法
1万
查看次数

标签 统计

dart ×1

flutter ×1

http ×1

localhost ×1