【问题标题】:Flutter HTTP Get Request Wrong PortFlutter HTTP 获取请求错误的端口
【发布时间】:2019-05-04 21:22:36
【问题描述】:

我尝试向我的本地主机服务器执行 GET 请求。我在带有 android studio 的外部设备上运行颤振应用程序,而端点也在 PC 上运行的 XAMPP/php 服务器。现在是每次我运行 GET 请求时的问题,它在错误的端口上运行。我写在 url 端口 80 但颤振在 53555 或其他 53 上执行它... 我尝试更改网址,但没有任何改变。 这是代码和错误信息

var url = new Uri.http("192.168.2.23:80", "/login", {"username":username,"password":password});
print(url);
var client = http.Client();
http.Response response = await client.get(url);

print('Response status: ${response.statusCode}');
print('Response body: ${response.body}');

这是错误

[ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: SocketException: OS Error: Connection timed out, errno = 110, address = 192.168.2.23, port = 53695

【问题讨论】:

  • 如果您在 Android 模拟器上运行应用程序,您可能需要模拟器的端口转发。
  • 因为 53... 端口是 local 端口。您的操作系统使用的那个。 80 端口是 目标 端口。
  • 以及如何为模拟器设置 80 端口?

标签: dart flutter


【解决方案1】:

你应该整理一下fetch,把它改成async

Future<List<Map<String, dynamic>>> fetch() async {
  http.Response response = await http.get('192.168.2.23:80/login');
  if (response.statusCode != 200)
      return null;
  //......
}

PS:在Future&lt;&gt; 中,如果需要,只需将地图替换为您的列表类型即可。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-15
    • 1970-01-01
    • 2021-08-05
    相关资源
    最近更新 更多