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

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

Flutter parsing error after correct response

问题

Here's the translated content:

我是Flutter开发的初学者,目前正在使用Dio来处理API调用。目前在打印后,响应以正确的格式显示。但在解析会话中,我遇到了这个错误:“类型 'List<dynamic>' 不是 'Map<String, dynamic>' 的子类型”

我的模型类

```dart
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);
  }
}

响应处理类

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) {
    // ...
  }

  // 打印(response.data.toString()) 将会输出:
  // [{id: 1, name: test}, {id: 2, name: MiddleClass}, {id: 3, name: UpperMiddleClass}, {id: 4, name: Rich}]
  // 这是正确格式的JSON数组。有人知道解决方法请帮忙...
}

I've translated the provided code and text. If you need any further assistance or have specific questions about it, feel free to ask.

<details>
<summary>英文:</summary>

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;**




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);
}

}


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) {



print(response.data.toString()); will prints : 
**[{id: 1, name: test}, {id: 2, name: MiddleClass}, {id: 3, name: UpperMiddleClass}, {id: 4, name: Rich}]** 
which is Json Array in correct format.
Anybody knows a solution pls help....

</details>


# 答案1
**得分**: 1

从 Dio 得到的结果是一个列表(List)的映射(Map)(而不是一个映射(Map)),它可能是一个List&lt;Map&lt;String, dynamic&gt;&gt;。

您需要将其中的每个项目转换为`BottomNavListItem`,类似于以下方式:

``` dart
List&lt;BottomNavListItem&gt;? bottomNavListModel;

bottomNavListModel = response.data
    .map((e) =&gt; Mapper.fromJson(e).toObject&lt;BottomNavListItem&gt;()!)
    .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:

List&lt;BottomNavListItem&gt;? bottomNavListModel;

bottomNavListModel = response.data
    .map((e) =&gt; Mapper.fromJson(e).toObject&lt;BottomNavListItem&gt;()!)
    .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

你需要像这样解析数据:

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

并且像这样更新模型:

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);
  }

  BottomNavListItem.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    name = json['name'];
  }
}
英文:

you need to parse the data like this

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

and update model like this...

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

  @override
  void mapping(Mapper map) {
    map(&quot;id&quot;, id, (v) =&gt; id = v);
    map(&quot;name&quot;, name, (v) =&gt; name = v);
  }

  BottomNavListItem.fromJson(Map&lt;String, dynamic&gt; json) {
    id = json[&#39;id&#39;];
    name = json[&#39;name&#39;];
  }
}

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:

确定