class CatalogModel {
static var items = [
Item(
id: 1,
name: "iPhone 12 Pro",
desc: "Apple iPhone 12th generation",
price: 999,
color: "#33505a",
image:
"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRISJ6msIu4AU9_M9ZnJVQVFmfuhfyJjEtbUm3ZK11_8IV9TV25-1uM5wHjiFNwKy99w0mR5Hk&usqp=CAc")
];
}
class Item {
final int id;
final String name;
final String desc;
final num price;
final String color;
final String image;
Item(
{required this.id,
required this.name,
required this.desc,
required this.price,
required this.color,
required this.image});
factory Item.fromMap(Map<String,dynamic> map){
Item(
id: map["id"],
name: map["name"],
desc: map["desc"],
price: map["price"],
color: map["color"],
image: map["image"],
);
}
toMap()=>{
"id":id,
"name":name,
"desc":desc,
"price":price,
"color":color,
"image":image,
}
}
我收到此錯誤....正文可能正常完成,導致回傳“null”,但回傳型別“Item”可能是不可為 null 的型別。嘗試在末尾添加 return 或 throw 陳述句
uj5u.com熱心網友回復:
Item您創建了一個工廠,當您應該回傳一個類時它不回傳任何內容。
您可以使用以下代碼更新您的工廠方法:
factory Item.fromMap(Map<String,dynamic> map) => Item(
id: map["id"],
name: map["name"],
desc: map["desc"],
price: map["price"],
color: map["color"],
image: map["image"],
);
```
uj5u.com熱心網友回復:
我找到了兩個修復:
在中添加回傳關鍵字
Item.fromMapreturn Item( id: map["id"], name: map["name"], ... );冒號結尾缺失
toMap
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/450901.html
