flutter - 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);
}));
有什么解决办法吗?谢谢
解决方案
我建议你尝试使用 dio 包(https://pub.dev/packages/dio),它在我的网络项目中工作。
推荐阅读
- android - 执行 A/B 测试时何时为 Firebase 获取 RemoteConfig
- css - wordpress 古腾堡 alignwide 和 alignfull 问题
- java - Quartz 作业未通过 XMLSchedulingDataProcessorPlugin 启动
- angular - 重定向到 Angular 应用程序之外但在同一个域中的 URL
- python - wxPython FileDialog 防止某些文件被拾取
- angular4-forms - 在 Angular-4 中单击 AddMore 按钮验证模板驱动的表单
- javascript - 如何通过收音机和 Javascript 在 HTML 中使用更改位置
- ionic-framework - 在 ipcRenderer.on 事件处理程序中设置全局变量在电子上运行离子不起作用
- compiler-construction - ocaml编译器是使用编译器前端模块parse来解析源码还是使用ocamlyacc来解析源码?
- html-table - 原生 HTML 和 zk-if 的奇怪错误