英文:
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 **"type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>'"**
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<Map<String, dynamic>>。
您需要将其中的每个项目转换为`BottomNavListItem`,类似于以下方式:
``` dart
List<BottomNavListItem>? bottomNavListModel;
bottomNavListModel = response.data
.map((e) => Mapper.fromJson(e).toObject<BottomNavListItem>()!)
.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<BottomNavListItem>? bottomNavListModel;
bottomNavListModel = response.data
.map((e) => Mapper.fromJson(e).toObject<BottomNavListItem>()!)
.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("id", id, (v) => id = v);
map("name", name, (v) => name = v);
}
BottomNavListItem.fromJson(Map<String, dynamic> json) {
id = json['id'];
name = json['name'];
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论