flutter - 将json解析成flutter List到地图
问题描述
您好,我想解析 Json,我遇到了这个问题,有什么问题请帮助
错误是 'List' 不是类型 'Map<String, dynamic>'
模型的子类型
class CategoriesModel {
CategoriesModel({
this.id,
this.title,
this.price,
this.description,
this.image,
});
int? id;
String? title;
double? price;
String? description;
String? image;
factory CategoriesModel.fromJson(Map<String, dynamic> json) {
return CategoriesModel(
id: json["id"],
title: json["title"],
price: json["price"].toDouble(),
description: json["description"],
image: json["image"],
);
}
Map<String, dynamic> toMap() => {
"id": id,
"title": title,
"price": price,
"description": description,
"image": image,
};
}
我做了这个(AppCubit.dart )
CategoriesModel? categoriesModel ;
getCategories(){
emit(OnLoadingCategoriesState());
DioHelper.getData(pathUrl: "products").then((value){
categoriesModel = CategoriesModel.fromJson(value.data);
// catList = (value.data as List)
// .map((x) => CategoriesModel.fromJson(x))
// .toList();
// print(catList.length);
emit(OnSuccessCategoriesState());
}).catchError((error){
emit(OnCatchErrorCategoriesState(error.toString()));
print(error.toString());
});
}
在Dio Helper 我做了通用功能来使用它
static Future<Response> getData({required pathUrl})async{
return await dio.get(pathUrl);
}
这是来自json的小数据
[
{
"id": 1,
"title": "Fjallraven - Foldsack No. 1 Backpack, Fits 15 Laptops",
"price": 109.95,
"description": "Your perfect pack for everyday use and walks in the forest. Stash your laptop (up to 15 inches) in the padded sleeve, your everyday",
"category": "men's clothing",
"image": "https://fakestoreapi.com/img/81fPKd-2AYL._AC_SL1500_.jpg"
},
{
"id": 2,
"title": "Mens Casual Premium Slim Fit T-Shirts ",
"price": 22.3,
"description": "Slim-fitting style, contrast raglan long sleeve, three-button henley placket, light weight & soft fabric for breathable and comfortable wearing. And Solid stitched shirts with round neck made for durability and a great fit for casual fashion wear and diehard baseball fans. The Henley style round neckline includes a three-button placket.",
"category": "men's clothing",
"image": "https://fakestoreapi.com/img/71-3HjGNDUL._AC_SY879._SX._UX._SY._UY_.jpg"
},
]
最后这是我出现的错误
I/flutter (31455): onChange -- AppCubit, Change { currentState: Instance of 'OnLoadingCategoriesState', nextState: Instance of 'OnCatchErrorCategoriesState' }
I/flutter (31455): type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>'
任何帮助我都会感激
解决方案
CategoriesModel.fromJson
期待类型Map<String, dynamic>
,但 json 片段显示响应是List
.
更新.then
处理列表的函数并CategoriesModel
从 中的每个项目创建List
,类似于上面代码片段中注释掉的代码。
DioHelper.getData(pathUrl: "products").then((List values) {
values.map((e) => CategoriesModel.fromJson(e)).toList();
}
推荐阅读
- flutter - 是否可以计算firestore中文档中有多少张地图?如果是,是否可以使用 ListView.builder?
- ruby - 将 sqlite3 与 Ruby 一起使用,有没有办法将每一行读取为哈希而不是数组?
- ajax - 带有图像 base64 的 Ajax 请求在 Laravel 中显示 403 Forbidden
- php - 如何在 php 中使用正则表达式从 html 数据中获取一些值
- python-3.x - Anaconda Python Jupyter Notebook 中的内核错误
- c# - 如何使用存储过程结果在 ASP.NET MVC 中将列表项作为 JSON 返回?
- ftp - 在 Ballerina Integrator 中 JSON 到 XML 和上传到 FTP 的问题
- openxml - 仅实施 ISO-29500 最新修订版以正确使用几乎所有 XLSX 文档就足够了吗?
- javascript - 链接到网站内另一个页面的特定 HTML 选项卡
- sql - 具有分析功能的 oracle 查询以根据条件设置行号