未处理的异常:类型 'List<dynamic>' 不是类型 'List<imageObject>?' 的子类型 |如何在 Flutter 模型中输入对象数组
Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'List<imageObject>?' | How to input Array of Objects in Model firebase in flutter
我一直在尝试从 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
但这不起作用
您还需要反序列化 imageObject json 数据。为此,您需要像为ProductModel class.
这是您需要的代码:
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 键的数据作为一个列表,并通过 imageObject 方法的 json 构造函数映射每个单独的元素。
您必须将收到的列表映射到 imageObject
s 列表。
- 为您的
imageObject
class 创建一个 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'];
}
您可能需要添加一些转换或对以前的代码进行一些修改才能使其正常工作,但这是一般的想法。
我一直在尝试从 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
但这不起作用
您还需要反序列化 imageObject json 数据。为此,您需要像为ProductModel class.
这是您需要的代码:
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 键的数据作为一个列表,并通过 imageObject 方法的 json 构造函数映射每个单独的元素。
您必须将收到的列表映射到 imageObject
s 列表。
- 为您的
imageObject
class 创建一个 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'];
}
您可能需要添加一些转换或对以前的代码进行一些修改才能使其正常工作,但这是一般的想法。