如何解决为什么 dart/flutter 无法从 REST API 解码长 json?
我使用带有 http 和 Dio 包的 Flutter 框架。在我的 REST API 中,我喜欢这个数据数组:
[
{
"id": 1,"title": "Абрикос","description": null,"status": "inactive","crop_type_id": 1,"user_id": null,"admin_id": null,"created_at": null,"updated_at": null
}
]
Dart 可以像这个数组项一样从 1 到 260 进行解码而不会出现任何错误。但是当我发送超过 260 个数组项时,我收到消息错误:
I/flutter (11048): Unexpected character
E/flutter (11048): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: Exception: Unexpected character
但是当我尝试使用 DartPad 网站对此类项目进行解码时,会成功解码项目。
这是我使用 Dio 包的代码:
Future get(String url) async {
Response response = await dio.get(
url,options: Options(
responseType: ResponseType.plain
)
);
JsonCodec codec = new JsonCodec();
if (response.data.isNotEmpty) {
return codec.decode(response.data);
}
}
这是我使用 http 包的示例请求:
http.Response response = await http.get(url,headers: {'Content-Type': 'application/json'});
List<dynamic> responseJson = json.decode(utf8.decode(response.bodyBytes));
以下是我如何在我的 Flutter 应用程序中调用请求的示例:
Future getCategories() async {
return await apiDataService.getSeedlings();
}
@override
void initState() {
WidgetsBinding.instance.addPostFrameCallback((_) async {
await getCategories();
});
super.initState();
}
我的代码中有错误,或者我必须配置 Flutter 才能接受来自 API 的长响应数据?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。