嗨,我的代码出现了一些错误,我搞不清楚,我尝试了很多步骤,但是它没有变化,我在提供者中调用的数据正在工作,但是它在页面中仍然是空的--这是代码
这是我的菜单模型
import 'package:kavarna/pages/models/gallery_model.dart';
class MenuModel {
int? id;
String? name;
double? price;
String? description;
CategoryModel? category;
DateTime? createdAt;
DateTime? updatedAt;
List<GalleryModel>? gallery;
MenuModel({
this.id,
this.name,
this.price,
this.description,
this.category,
this.createdAt,
this.updatedAt,
this.gallery,
});
MenuModel.fromJson(Map<String, dynamic> json) {
id = json['id'];
name = json['name'];
price = double.parse(json['price'].toString());
description = json['description'];
category = CategoryModel.fromJson(json['category']);
gallery = json['gallery']
.map<GalleryModel>((gallery) => GalleryModel.formJson(json))
.toList();
createdAt = DateTime.parse(json['created_at']);
updatedAt = DateTime.parse(json['updated_at']);
}
Map<String, dynamic> toJson() {
return {
'id': id,
'name': name,
'price': price,
'description': description,
'category': category?.toJson(),
'gallery': gallery?.map((e) => e.toJson()).toList(),
'created_at': createdAt,
'updated_at': updatedAt,
};
}
}这是我的供应商
import 'package:kavarna/pages/models/menu_model.dart';
import 'package:kavarna/pages/services/menu_service.dart';
class MenuProvider with ChangeNotifier {
List<MenuModel> _menuries = [];
List<MenuModel> get menuries => _menuries;
set menuries(List<MenuModel> menuries) {
_menuries = menuries;
notifyListeners();
}
Future<void> getMenus() async {
try {
List<MenuModel> menuries = await MenuService().getMenu();
_menuries = menuries;
} catch (e) {
print(e);
}
}
}如果问题出在哪里,但它仍然显示错误,就像我发布的标题一样,我试着评论打印(E);在我的提供者中,它消失了,但如果我没有,它仍然显示--错误就像在这张照片上一样。

关于我在category_model中得到的更多细节和错误,我写错了代码吗?
class CategoryModel {
int? id;
String? name;
CategoryModel({
this.id,
this.name,
});
CategoryModel.fromJson(Map<String, dynamic> json) {
id = json['id'];
name = json['name'];
}
Map<String, dynamic> toJson() {
return {
'id': id,
'name': name,
};
}
}发布于 2022-01-16 17:50:47
您在这里映射了错误的变量:
.map<GalleryModel>((gallery) => GalleryModel.formJson(json))这样试一试:
.map<GalleryModel>((gallery) => GalleryModel.formJson(gallery))编辑
此外,根据你的答复:

我没有看到category字段,但是您正在尝试用json['category']映射您的category。唯一的分类是categories_id和categories,也许你指的是json['categories']?
https://stackoverflow.com/questions/70732614
复制相似问题