我一直在嘗試從firebase實際獲取資料所以實際資料是:

如何獲取 imageUrl ?
我的嘗試是:
class ProductModel {
String? name;
String? price;
String? discountPrice;
String? discountRate;
String? category;
String? description;
List<imageObject>? image;
ProductModel(
{required this.name,
required this.price,
this.category,
this.description,
this.discountPrice,
this.discountRate,
this.image});
ProductModel.fromMap(Map<String, dynamic> data) {
name = data['name'];
// image = data['imageUrls'][0]['url']; // To get single image i do this
image = data['imageUrls']; // but this is not working
category = data['category'];
description = data['Description'];
price = data['price'];
discountPrice = data['discountPrice'];
discountRate = data['discountRate'];
}
}
class imageObject {
final String public_id;
final String url;
imageObject({
required this.public_id,
required this.url,
});
}
它給出了例外:Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'List<imageObject>?'
并訪問我正在做的第一張圖片,
product.image![0].url,
其中產品的型別是 ProductModel
但這不起作用
uj5u.com熱心網友回復:
您還需要反序列化imageObject json 資料。為此,您需要將工廠 imageObject.fromJson()建構式添加到您的imageObject類,就像您為 ProductModel 類所做的那樣。
這是您需要的代碼:
class ProductModel {
String? name;
String? price;
String? discountPrice;
String? discountRate;
String? category;
String? description;
List<imageObject>? image;
ProductModel({
required this.name,
required this.price,
this.category,
this.description,
this.discountPrice,
this.discountRate,
this.image,
});
factory ProductModel.fromJson(Map<String, dynamic> jsonData) => ProductModel(
name: jsonData['name'] as String?,
price: jsonData['price'] as String?,
category: jsonData['category'] as String?,
description: jsonData['Description'] as String?,
discountPrice: jsonData['discountPrice'] as String?,
discountRate: jsonData['discountRate'] as String?,
//Have a good look here to understand how nested list of maps are deserialized
image: (jsonData['imageUrls'] as List<dynamic>?)
?.map((e) => imageObject.fromJson(e as Map<String, dynamic>))
.toList(),
);
}
class imageObject {
final String public_id;
final String url;
imageObject({
required this.public_id,
required this.url,
});
factory imageObject.fromJson(Map<String, dynamic> jsonData) => imageObject(
public_id: jsonData['public_id'] as String,
url: jsonData['url'] as String,
);
}
我們在這里所做的是,將 imageUrls 鍵中的資料作為 List 并通過 imageObject 方法的 json 建構式映射每個單獨的元素。
uj5u.com熱心網友回復:
您必須將接收串列映射到imageObjects 串列。
imageObject為你的類創建一個 fromMap 建構式
class imageObject {
final String public_id;
final String url;
imageObject({
required this.public_id,
required this.url,
});
imageObject.fromMap(Map<String, dynamic> map) => imageObject(public_id = map['public_id'], url = map['url'] );
}
- 像下面這樣使用它:
ProductModel.fromMap(Map<String, dynamic> data) {
name = data['name'];
image = data['imageUrls'].map((map) => imageObject.fromMap(map) );
category = data['category'];
description = data['Description'];
price = data['price'];
discountPrice = data['discountPrice'];
discountRate = data['discountRate'];
}
您可能需要添加一些轉換或對以前的代碼進行一些修改以使其作業,但這是一般的想法。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/463606.html
上一篇:如何將一個類的實體添加到另一個類的陣列中(Ruby)
下一篇:如何從嵌套物件重新計算物件?
