未经手的异常:type' list< dynamic>'不是类型的子类型。 |如何在模型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,
});
}
它给出了例外:未经手的例外:键入'list&lt; dynamic&gt;' ”的子类型
不是
product.image![0].url,
类型
的“列表
I have been trying to actually get the data from firebase
So the actual data is :
How to get the imageUrl ?
My Try is :
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,
});
}
It gives exception :Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'List<imageObject>?'
And to access the first image i am doing,
product.image![0].url,
where product is of type ProductModel
But this is not working
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您还需要对 imageObject JSON数据进行估算。为此,您需要将 factory imageObject.fromjson()构造函数添加到 imageObject 类中,就像您为ProductModel类所做的那样。
这是您需要的代码:
我们在这里所做的是,将来自ImageUrls键的数据作为列表,并通过ImageObject方法的JSON构造函数映射每个单独的元素。
You need to deserialize the imageObject json data as well. For that you need to add the factory imageObject.fromJson() constructor to your imageObject class just like you did for the ProductModel class.
Here's the code you need:
What we did here is, take the data from imageUrls key as a List and map every individual element thru the json constructor of the imageObject method.
您必须将重新列表映射到
ImageObject
s列表。imageObject
类创建图形构造函数您可能需要添加一些铸件或对先前的代码进行一些修改使其正常工作,但这是一般的想法。
You have to map the recived list to an
imageObject
s list.imageObject
classYou may need to do add some casting or do some modifications to the previous code make it work, but this is the general idea.