嗨,我的代碼出現了一些錯誤,我無法弄清楚,我已經嘗試了很多步驟,但它仍然保持不動,我在提供程式中呼叫的資料正在作業,但它在頁面中仍然為空 這是代碼
這是我的選單模型
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);
}
}
}
idk問題出在哪里,但仍然顯示錯誤,例如我發布的標題,我嘗試評論列印(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,
};
}
}
uj5u.com熱心網友回復:
您在這里映射錯誤的變數:
.map<GalleryModel>((gallery) => GalleryModel.formJson(json))
試試這種方式:
.map<GalleryModel>((gallery) => GalleryModel.formJson(gallery))
編輯
此外,根據您的回復:

我沒有看到任何category欄位,但是您正在嘗試category使用json['category']. 唯一要分類的是categories_idand categories,也許你的意思是json['categories']?
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/412695.html
標籤:
