首页 > 解决方案 > Flutter Http post call 不适用于网络,仅适用于移动设备

问题描述

  loginUser(email, password) async {
    var url = Uri.parse
        .call("https://us-central1-agenie-webapp.cloudfunctions.net/api/login");
    final http.Response response = await http.post(
      url,
      headers: <String, String>{
        'Content-Type': 'application/json',
      },
      body: jsonEncode(<String, String>{'email': email, 'password': password}),
    );
    if (response.statusCode == 200) return response.body;
    return null;
  }

这是当我在 web localhost 上使用 flutter run 运行它时的 api post 调用,当我单击登录按钮时调用此函数,按下它会在 vscode 中打开一个名为 browser_client.dart 的窗口并转到第 69 行,其中显示:

 unawaited(xhr.onError.first.then((_) {
      // Unfortunately, the underlying XMLHttpRequest API doesn't expose any
      // specific information about the error itself.
      completer.completeError(
          ClientException('XMLHttpRequest error.', request.url),
          StackTrace.current);
    }));

有什么解决办法吗?谢谢

标签: flutterhttpdartweb

解决方案


我建议你尝试使用 dio 包(https://pub.dev/packages/dio),它在我的网络项目中工作。


推荐阅读