2

这是我在尝试将网络请求的响应正文解析为 json 映射时仅在发布模式下遇到的错误(奇怪)。我记下了代码以及下面的错误堆栈跟踪。我应该提到的另一件奇怪的事情是它有时只会发生。不是所有的时间。但是对于我在市场上发布的应用程序,当我在 firebase 上查看崩溃报告时,也会报告此错误。(虽然它不会导致崩溃)

我应该提到我使用 Dio 包作为 http 客户端

更新: 我发现它有时会发生,但是当它发生时,当我一遍又一遍地发送相同的请求或有时其他请求时,它会一直发生。直到我退出应用程序并再次运行它。

错误堆栈跟踪:

I/flutter (31714): type '_OneByteString' is not a subtype of type 'Map<String, dynamic>'
I/flutter (31714): #0      Server.getUser (package:mehrgan/data/network/server.dart:83)
I/flutter (31714): <asynchronous suspension>
I/flutter (31714): #1      Future.wait.<anonymous closure> (dart:async/future.dart:0)
I/flutter (31714): <asynchronous suspension>
I/flutter (31714): type '_OneByteString' is not a subtype of type 'Map<String, dynamic>'
I/flutter (31714): #0      Server.getCourses (package:mehrgan/data/network/server.dart:101)
I/flutter (31714): <asynchronous suspension>
I/flutter (31714): #1      Future.wait.<anonymous closure> (dart:async/future.dart:0)
I/flutter (31714): <asynchronous suspension>

我的代码:

Response response = await _dio.get('user/info');
Map<String, dynamic> json = jsonDecode(jsonEncode(response.data));
4

1 回答 1

3

我在使用 Dart/Flutter 和 Dio 客户端库时遇到了这个问题。

服务器端我从 PHP 脚本请求数据。

我通过在我的 PHP 脚本中添加以下行来解决此问题:

header('Content-Type: application/json');

非常感谢上面的 Thiyraash David,他给了我解决问题所需的线索。

于 2020-11-13T22:30:44.343 回答