“Flutter解析错误在正确响应后”

huangapple go评论101阅读模式
英文:

Flutter parsing error after correct response

问题

Here's the translated content:

  1. 我是Flutter开发的初学者,目前正在使用Dio来处理API调用。目前在打印后,响应以正确的格式显示。但在解析会话中,我遇到了这个错误:“类型 'List<dynamic>' 不是 'Map<String, dynamic>' 的子类型”
  2. 我的模型类
  3. ```dart
  4. import 'package:object_mapper/object_mapper.dart';
  5. class BottomNavListItem with Mappable {
  6. int? id;
  7. String? name;
  8. @override
  9. void mapping(Mapper map) {
  10. map("id", id, (v) => id = v);
  11. map("name", name, (v) => name = v);
  12. }
  13. }

响应处理类

  1. Future<List<BottomNavListItem>?> getFamilyStatus(String url) async {
  2. Response response;
  3. List<BottomNavListItem>? bottomNavListModel;
  4. try {
  5. response = await WebUtil.createDio().get(url);
  6. if (kDebugMode) {
  7. print(response.data.toString());
  8. }
  9. bottomNavListModel =
  10. Mapper.fromJson(response.data).toObject<List<BottomNavListItem>>();
  11. } on DioError catch (e) {
  12. // ...
  13. }
  14. // 打印(response.data.toString()) 将会输出:
  15. // [{id: 1, name: test}, {id: 2, name: MiddleClass}, {id: 3, name: UpperMiddleClass}, {id: 4, name: Rich}]
  16. // 这是正确格式的JSON数组。有人知道解决方法请帮忙...
  17. }
  1. I've translated the provided code and text. If you need any further assistance or have specific questions about it, feel free to ask.
  2. <details>
  3. <summary>英文:</summary>
  4. Am a beginner in flutter development and am currently using dio for handling api calls. Currently the response is showing in the correct format after printing it. But in the parsing session I got error like this **&quot;type &#39;List&lt;dynamic&gt;&#39; is not a subtype of type &#39;Map&lt;String, dynamic&gt;&#39;&quot;**
  5. my model class

import 'package:object_mapper/object_mapper.dart';

class BottomNavListItem with Mappable {
int? id;
String? name;

@override
void mapping(Mapper map) {
map("id", id, (v) => id = v);
map("name", name, (v) => name = v);
}

}

  1. response handling class

Future<List<BottomNavListItem>?> getFamilyStatus(String url) async {
Response response;
List<BottomNavListItem>? bottomNavListModel;
try {
response = await WebUtil.createDio().get(url);
if (kDebugMode) {
print(response.data.toString());
}
bottomNavListModel =
Mapper.fromJson(response.data).toObject<List<BottomNavListItem>>();
} on DioError catch (e) {

  1. print(response.data.toString()); will prints :
  2. **[{id: 1, name: test}, {id: 2, name: MiddleClass}, {id: 3, name: UpperMiddleClass}, {id: 4, name: Rich}]**
  3. which is Json Array in correct format.
  4. Anybody knows a solution pls help....
  5. </details>
  6. # 答案1
  7. **得分**: 1
  8. Dio 得到的结果是一个列表(List)的映射(Map)(而不是一个映射(Map)),它可能是一个List&lt;Map&lt;String, dynamic&gt;&gt;。
  9. 您需要将其中的每个项目转换为`BottomNavListItem`,类似于以下方式:
  10. ``` dart
  11. List&lt;BottomNavListItem&gt;? bottomNavListModel;
  12. bottomNavListModel = response.data
  13. .map((e) =&gt; Mapper.fromJson(e).toObject&lt;BottomNavListItem&gt;()!)
  14. .toList();

请注意,这里的翻译只包括代码部分,不包括问题或其他内容。

英文:

The result that you got from the Dio is a List of Maps (not a Map), which may be a List<Map<String, dynamic>>.

You need to convert each of its items to BottomNavListItem with something like this:

  1. List&lt;BottomNavListItem&gt;? bottomNavListModel;
  2. bottomNavListModel = response.data
  3. .map((e) =&gt; Mapper.fromJson(e).toObject&lt;BottomNavListItem&gt;()!)
  4. .toList();

答案2

得分: 1

以下是已翻译的内容:

得到的响应是一个 Map 列表。

因此,你不能直接在响应上使用 Mapper.fromJson(response.data)

你可以改为遍历每个项目并调用 Mapper.fromJson(response[i].data),也许这会有所帮助。

或者为了更容易调试,你可以尝试使用 print(response.runTimeType) 来查找响应的运行时类型。

英文:

The response that you are getting is list of a Map

So you can't do Mapper.fromJson(response.data) directly on response .

You can instead iterate through each item and call Mapper.fromJson(response[i].data) may this will help.

or to debug more easily you can try to find out runTimeType of your response
by using print(response.runTimeType)

答案3

得分: 1

你需要像这样解析数据:

  1. response.data.forEach((item){
  2. bottomNavListModel?.add(BottomNavListItem.fromJson(item));
  3. });

并且像这样更新模型:

  1. class BottomNavListItem with Mappable {
  2. int? id;
  3. String? name;
  4. @override
  5. void mapping(Mapper map) {
  6. map("id", id, (v) => id = v);
  7. map("name", name, (v) => name = v);
  8. }
  9. BottomNavListItem.fromJson(Map<String, dynamic> json) {
  10. id = json['id'];
  11. name = json['name'];
  12. }
  13. }
英文:

you need to parse the data like this

  1. response.data.forEach((item){
  2. bottomNavListModel?.add(BottomNavListItem.fromJson(item));
  3. });

and update model like this...

  1. class BottomNavListItem with Mappable {
  2. int? id;
  3. String? name;
  4. @override
  5. void mapping(Mapper map) {
  6. map(&quot;id&quot;, id, (v) =&gt; id = v);
  7. map(&quot;name&quot;, name, (v) =&gt; name = v);
  8. }
  9. BottomNavListItem.fromJson(Map&lt;String, dynamic&gt; json) {
  10. id = json[&#39;id&#39;];
  11. name = json[&#39;name&#39;];
  12. }
  13. }

huangapple
  • 本文由 发表于 2023年4月17日 19:05:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76034471.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定