【问题标题】:Flutter http Request Error on Localhost http://127.0.0.1:8000/api/users本地主机上的 Flutter http 请求错误 http://127.0.0.1:8000/api/users
【发布时间】:2020-04-24 12:10:03
【问题描述】:

你好朋友,我是新来的,当我尝试在本地主机上发出 http 请求时,我收到了这个错误

E/flutter (8392): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] 未处理异常:SocketException:操作系统错误:连接被拒绝,errno = 111,地址 = 127.0.0.1,端口 = 33200 我在本地主机上使用带有 laravel 应用程序的 http pacakge

Future<List<User>> getUsers({int pageNumber = 1}) async {
    var url = "http://127.0.0.1:8000/api/users";
    final res = await http.get(url);
    if (res.statusCode == 200) {
      return compute(parseUsers, res.body);
    } else {
      throw Exception("error in fetch userslist data");
    }
  }
}


List<User> parseUsers(String responseBody) {
  final items = jsonDecode(responseBody)["Data"];
  List<User> data = [];
  for (var item in items) {
    data.add(User.fromJson(item));
  }
  return data;
}

【问题讨论】:

    标签: http flutter localhost


    【解决方案1】:

    您可以尝试使用ngrok 来虚拟化您的本地 API。

    1 - 在网络掩码 IP 上为您的应用程序提供服务。假设您在 API 上使用 Laravel,请使用以下命令:
    php artisan --host 192.168.0.**

    2 - 使用 ngrok 虚拟化您的 API:
    ngrok http 192.18.0.**:8000

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      • 2018-12-04
      • 1970-01-01
      • 2015-09-02
      • 1970-01-01
      • 2017-07-23
      相关资源
      最近更新 更多