从Firebase检索数据时发生类型转换错误
我目前在从Firebase检索products类型为ProductData的特定字段的数据时遇到问题,这是givng me cast错误。我想知道如何将Firebase映射转换为我的ProductData,ProductData是一类产品。下面是我的代码。 ProductData从Firebase检索数据时发生类型转换错误,firebase,flutter,google-cloud-firestore,Firebase,Flutter,Google Cloud Firestore,我目前在从Firebase检索products类型为ProductData的特定字段的数据时遇到问题,这是givng me cast错误。我想知道如何将Firebase映射转换为我的ProductData,ProductData是一类产品。下面是我的代码。 ProductData class ProductData{ String category; String description; String id; String images; bool isFeatured;
class ProductData{
String category;
String description;
String id;
String images;
bool isFeatured;
bool isPopular;
bool isPromotion;
String name;
double price;
int quantity;
ProductData({
@required this.category,
@required this.description,
@required this.id,
@required this.images,
@required this.isFeatured,
@required this.isPopular,
@required this.isPromotion,
@required this.name,
@required this.price,
@required this.quantity,
});
ProductData.fromDocument(DocumentSnapshot documentSnapshot){
id=documentSnapshot.data()['id'];
category= documentSnapshot.data()['category'];
description= documentSnapshot.data()['description'];
images= documentSnapshot.data()['images'];
isFeatured = documentSnapshot.data()['isFeatured'];
isPopular= documentSnapshot.data()['isPopular'];
isPromotion= documentSnapshot.data()['isPromotion'];
name = documentSnapshot.data()['name'];
price = documentSnapshot.data()['price']+.0;
quantity=documentSnapshot.data()['quantity'];
}
Map<String, dynamic> toMap() => {
"id":id,
"category": category,
"descripton": description,
"imgUrl": images,
"isFeatured":isFeatured,
"isPopular":isPopular,
"isPromotion":isPromotion,
"name":name,
"price":price,
"quantity":quantity,
};
Map<String, dynamic> toResumeMap(){
return {
"id":id,
"category": category,
"descripton": description,
"imgUrl": images,
"isFeatured":isFeatured,
"isPopular":isPopular,
"isPromotion":isPromotion,
"name":name,
"price":price,
"quantity":quantity,
};
}
factory ProductData.fromMap(Map<String, dynamic> json) => ProductData(
id: json["id"],
name: json["name"],
description: json["description"],
price: json["price"]+0.0,
);
}
类ProductData{
字符串类别;
字符串描述;
字符串id;
字符串图像;
布尔是特色;
布尔是流行的;
bool-isPromotion;
字符串名;
双倍价格;
整数;
产品数据({
@需要此类别,
@需要此说明,
@需要这个.id,
@需要这个。图像,
@此项为必填项,
@需要这个。我很流行,
@需要此项。i建议,
@需要此名称,
@需要这个价格,
@所需数量,
});
ProductData.fromDocument(DocumentSnapshot DocumentSnapshot){
id=documentSnapshot.data()['id'];
category=documentSnapshot.data()['category'];
description=documentSnapshot.data()['description'];
images=documentSnapshot.data()['images'];
isFeatured=documentSnapshot.data()['isFeatured'];
isPopular=documentSnapshot.data()['isPopular'];
isPromotion=documentSnapshot.data()['isPromotion'];
name=documentSnapshot.data()['name'];
price=documentSnapshot.data()['price']+.0;
数量=documentSnapshot.data()['quantity'];
}
映射toMap()=>{
“id”:id,
“类别”:类别,
“描述符”:描述,
“imgUrl”:图像,
“isFeatured”:isFeatured,
“isPopular”:isPopular,
“isPromotion”:isPromotion,
“姓名”:姓名,
“价格”:价格,
“数量”:数量,
};
映射到消费地图(){
返回{
“id”:id,
“类别”:类别,
“描述符”:描述,
“imgUrl”:图像,
“isFeatured”:isFeatured,
“isPopular”:isPopular,
“isPromotion”:isPromotion,
“姓名”:姓名,
“价格”:价格,
“数量”:数量,
};
}
factory ProductData.fromMap(Map json)=>ProductData(
id:json[“id”],
名称:json[“名称”],
description:json[“description”],
价格:json[“价格”]+0.0,
);
}
问题出在购物车产品上,我在给出错误的行旁边放了一条评论。
省道
class CartProduct {
double totalPrice;
String deliveryLocation;
String userId;
int quantity;
double subtotal;
String id;
ProductData products;
DateTime date;
CartProduct(
{
//@required this.totalPrice,
@required this.products,
@required this.deliveryLocation,
@required this.quantity,
@required this.date,
@required this.userId,
@required this.id,
@required this.subtotal
}
);
CartProduct.fromDoucment(DocumentSnapshot document){
date=document.data()['date'];
deliveryLocation = document.data()['deliveryLocation'];
id = document.data()['id'];
products = document.data()['products']; // Here is where my issue is
quantity = document.data()['quantity'];
subtotal= document.data()['subtotal'];
userId=document.data()['userId'];
}
//Now we must also to Write the Data Into DB Based on our request.
//Create a String Dynamic Function to write into the DB
Map<String, dynamic> toMap(){
return {
//'totalPrice': totalPrice,
'products': products.toResumeMap(),
'quantity': quantity,
'deliveryLocation': deliveryLocation,
//'quantity': quantity,
'userId': userId,
'date': date,
'id': id,
'subtotal': subtotal
};
}
Map<String, dynamic> toResumeMap(){
return {
"id":id,
"category": products.category,
"description": products.description,
"imgUrl": products.images,
"isFeatured":products.isFeatured,
"isPopular":products.isPopular,
"isPromotion":products.isPromotion,
"name":products.name,
"price":products.price,
};
}
factory CartProduct.fromMap(Map<String, dynamic> json) => CartProduct(
// totalPrice: json["totalPrice"],
quantity: json["quantity"],
deliveryLocation: json["deliveryLocation"],
userId: json["userId"],
products: json['products'],
date: json["date"],
id: json["id"],
subtotal: json['subtotal']
);
}
类产品{
双倍总价;
字符串传递位置;
字符串用户标识;
整数;
双倍小计;
字符串id;
产品数据产品;
日期时间日期;
手推车(
{
//@需要这个。总价,
@需要这个产品,
@需要此.deliveryLocation,
@所需数量,
@此日期为,
@需要此.userId,
@需要这个.id,
@需要这个。小计
}
);
CartProduct.FromDoucMont(文档快照文档){
日期=document.data()['date'];
deliveryLocation=document.data()['deliveryLocation'];
id=document.data()['id'];
products=document.data()['products'];//这是我的问题所在
数量=document.data()['quantity'];
小计=document.data()['subtotal'];
userId=document.data()['userId'];
}
//现在我们还必须根据请求将数据写入数据库。
//创建字符串动态函数以写入数据库
映射toMap(){
返回{
//“totalPrice”:totalPrice,
“产品”:products.toResumeMap(),
“数量”:数量,
“deliveryLocation”:deliveryLocation,
//“数量”:数量,
“userId”:userId,
“日期”:日期,
“id”:id,
“小计”:小计
};
}
映射到消费地图(){
返回{
“id”:id,
“类别”:产品。类别,
“说明”:产品说明,
“imgUrl”:产品、图像、,
“isFeatured”:products.isFeatured,
“isPopular”:products.isPopular,
“isPromotion”:products.isPromotion,
“名称”:products.name,
“价格”:产品、价格、,
};
}
factory CartProduct.fromMap(Map json)=>CartProduct(
//totalPrice:json[“totalPrice”],
数量:json[“数量”],
deliveryLocation:json[“deliveryLocation”],
userId:json[“userId”],
产品:json['products'],
日期:json[“日期”],
id:json[“id”],
小计:json['subtotal']
);
}
还可以在图像中找到我的Firebase字段。
提前感谢您,在CartProduct类中,products变量是一个类(ProductData),但在methode CartProduct.FromDoucm中,您可以直接在products变量中传递一些数据(传递动态数据)。您应该在之前使用数据(document.data()['products'])初始化ProductData。将变量更改为final。将CartProduct.FromDocument重写为:
factory CartProduct.fromDoucment(DocumentSnapshot document){
return CartProduct(
date: document.data()['date'],
deliveryLocation: document.data()['deliveryLocation'],
id: document.data()['id'],
products: document.data()['products'],
quantity: document.data()['quantity'],
subtotal: document.data()['subtotal'],
userId: document.data()['userId'],
);
}
这将返回CartProduct的新实例。Hi@Windsor\u Elliot,感谢您的反馈。我设法克服了这个问题,问题是我太傻了,需要将这些值映射到productdata类,比如productdata.fromMap(doc.data()['products');我太傻了。