无法从flutter Web应用程序中的API读取数据

问题描述 投票:0回答:1

我正在开发一个具有移动应用程序和网络应用程序的应用程序,我已经使用 MYSQL 数据库创建了自定义 Nodejs API,这些 API 在移动应用程序中运行良好。但在 flutter web 的情况下,我的 post API 工作正常,但我无法从 API 读取数据,该 API 在 postman 和移动应用程序中都工作正常,但是当我在 flutter web 中测试它时

我收到此错误响应。 [log] 获取产品时出错:ClientException: XMLHttpR…, uri=http://127.0.0.1:2000/api/v1/product/getall(使用 HTTP 包)

我还尝试了 Dio 包,但出现了相同的错误 [日志] 获取产品时出错:DioException [连接......这很可能无法通过库解决。

这是我获取数据的函数:

 Future<List<Product>> fetchProducts() async {
    log("hit");
    final url = Uri.parse(
        '$baseURL:2000/api/v1/product/getall'); // Replace with your local IP and port

    try {
      final response = await http.get(url);

      if (response.statusCode == 200) {
        final responseData = json.decode(response.body);
        final List<dynamic> data = responseData['data'];
        log('Response data: ${responseData['data']}');

        return data
            .map((productJson) => Product.fromJson(productJson))
            .toList();
      } else {
        log('Failed to load products. Status code: ${response.statusCode}');
        return [];
      }
    } catch (error) {
      log('Error fetching products: $error');
      return [];
    }
  }
NOTE: it works fine in Postman and mobile app, the issue is only in flutter web

我想读取数据并在我的网络应用程序中显示它们

flutter http exception
1个回答
0
投票

使用你的IP地址作为路径而不是127.0.0.1,这样就可以正常工作

© www.soinside.com 2019 - 2024. All rights reserved.