exception - Flutter web - 调用 http.get 抛出异常
问题描述
这在 Android/桌面上运行良好:
Future<List<City>> fetchCities() async {
final response =
await http.get(globals.url + '/city',
headers: {HttpHeaders.acceptHeader: globals.apiVersion});
if (response.statusCode == 200) {
// If the call to the server was successful, parse the JSON
return compute(parseCities, response.body);
} else {
// If that call was not successful, throw an error.
print('Failed to load cities');
throw TextException(Strings.msg_session_expired);
}
}
但是当作为 Web 应用程序启动时,我遇到了一个异常:
errors.dart:145 Uncaught (in promise) Error: Unsupported operation: Platform._version
at Object.throw_ [as throw] (errors.dart:194)
at Function._version (io_patch.dart:284)
at Function.get version [as version] (platform_impl.dart:121)
at get _version (platform.dart:74)
at Function.desc.get [as _version] (utils.dart:75)
at Function.get version [as version] (platform.dart:231)
at Object._getHttpVersion (http_impl.dart:3234)
at new _http._HttpClient.new (http_impl.dart:2071)
at Function.new (http.dart:1473)
at new io_client.IOClient.new (io_client.dart:23)
at Function.new (client.dart:30)
at _withClient (http.dart:165)
at _withClient.next (<anonymous>)
at runBody (async_patch.dart:84)
at Object._async [as async] (async_patch.dart:123)
at Object._withClient (http.dart:164)
at Object.get (http.dart:47)
at fetchCities (mcity.dart:113)
at fetchCities.next (<anonymous>)
at runBody (async_patch.dart:84)
at Object._async [as async] (async_patch.dart:123)
是否有任何方法可以使 http.get 请求在 flutter_web 上工作?
解决方案
推荐阅读
- c++ - 将 Cspice 与 mingw 一起使用
- c# - 检查用户是否通过 C# 中的 CalendarDatePicker 输入了日期时间值
- ios - 可以在 Safari 中访问原深感摄像头吗?
- scrapy - 刮擦 | 如何在没有 urllib 的情况下从请求中获得响应?
- sql - 用于证明候选键约束在 R(ABCD) 中成立的 SQL 语句
- java - 使用 Ionic 4 访问 JAR 文件中的 Java API
- node.js - Angular、电子、打字稿和机器人
- javascript - 为什么 Node.js 不能处理来自 Vue.js 的请求?
- haskell - 如何避免编写琐碎的数据类型检查函数
- c# - 在 C# 中使用带有数据库的组合框选择索引值