gpt4 book ai didi

http - 如何指向本地主机 :8000 with the Dart http package in Flutter?

转载 作者:IT老高 更新时间:2023-10-28 12:31:46 27 4
gpt4 key购买 nike

我正在关注 Flutter Networking/HTTP tutorial向在我的 localhost:8000 上运行的服务器发出 GET 请求。通过我的浏览器访问我的本地主机工作正常。我的代码如下所示:

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

当我指向任何真实的 URL(例如 https://example.com)时,这可以正常工作,但是当我指向 https://localhost:8000 时或 https://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 package code而且似乎没有办法为 URL 指定端口。如何指向我的本地主机?

最佳答案

10.0.2.2 替换字符串 localhost 为我解决了这个问题,因为我在 Android 模拟器中运行代码,该模拟器在 VM 中运行。它本质上是 this question 的副本.

关于http - 如何指向本地主机 :8000 with the Dart http package in Flutter?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47372568/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com