【问题标题】:Why http.get is throwing error in Flutter为什么 http.get 在 Flutter 中抛出错误
【发布时间】:2022-12-31 23:45:42
【问题描述】:

The error message that is showing in Dev tools我正在尝试在 Flutter 中执行一个 PHP 文件。 我的代码是:

class _MyAppState extends State<MyApp> {
  @override
  var url = 'http://localhost/myDashFolder/getdata.php';

  Future<List> getData() async {
    final response = await http.get(Uri.parse(url));
    if (response.statusCode == 200) {
      return json.decode(response.body);
    }
    else {
      throw Exception("Response content length is ${response.statusCode}, failed to get any details.");
    }
  }

在开发工具中,获取请求显示错误。但是,当我手动执行相同的操作时,它会起作用。

我究竟做错了什么?

【问题讨论】:

  • 你怎么运行这个?作为网络应用程序?在设备或模拟器上?作为桌面应用程序?什么它显示错误吗?
  • 如果它的 android 那么需要 internet 权限和 localhost 需要设备在同一个网络中
  • 我正在使用模拟器在 Android Studio 的桌面上运行它。错误消息 - 我在我的原始帖子中包含了一个链接。

标签: flutter http get


【解决方案1】:

【讨论】:

  • 只有您的计算机知道 localhost 一词在 URL 中的含义。因此,当模拟器充当外部设备时,它不知道本地主机映射到的 IP 地址。
  • 如果对您有帮助,您能否将答案标记为正确?
猜你喜欢
  • 2021-06-28
  • 1970-01-01
  • 2020-03-10
  • 1970-01-01
  • 2019-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-16
相关资源
最近更新 更多