firebase - 类型转换错误从 Firebase 检索数据
问题描述
我目前在从 Firebase 检索ProductData类型产品的特定字段上的数据时遇到问题,这给了我投射错误。我想知道如何将 Firebase 映射转换为我的 ProductData,它是 Products.Below 是我的代码。 产品数据
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,
);
}
问题出在购物车产品上,我在给我错误的行附近发表了评论。CartProduct.dart
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']
);
}
提前致谢
解决方案
Helo,在您的 CartProduct 类中, products 变量是一个类(ProductData),但在您的方法中 CartProduct.fromDoucment 您直接在 products 变量中传递一些数据(您传递一个动态数据)。您应该在之前使用数据 (document.data()['products']) 初始化 ProductData。
推荐阅读
- python - 你好世界python程序在同一台机器上的两个版本python
- java - 当我尝试独立启动 JBOSS 时出现 SIGSEGV Java 致命错误
- r - 从 R 中的数据框中识别无意义或乱码文本。有没有办法将字符串/单词部分匹配到字典?
- apache-spark - 无法在python中以纱线集群模式读取jceks文件
- javascript - 如何在 HTML 中使用 2D 画布创建平滑路径?
- flutter - 下拉刷新WebView页面
- ansible - Ansible:等待 ssh 可访问
- r - grep r中的多个字符
- html - 在 iPhone 上,具有“datepicker”类的 Input 元素不跨越整个列宽
- ios - 我们可以在 ios 中购买与礼品用户相同的产品代码吗?