英文:
Dart converting Firebase Object to Pojo Class
问题
以下是翻译好的内容:
我正在阅读以下 Firebase 文档:
collectionRef.get().then(
(querySnapshot) {
print("All Orders");
for (var docSnapshot in querySnapshot.docs) {
var _product = docSnapshot.data();
print(CartItem.fromJson(_product).company);
}
},
onError: (e) => print("Error completing: $e"),
);
print 语句正在打印值作为对象列表:
[{quantity: 1, price: null, model: TP WM TWT95-P102GB, company: Choose Company, id: 2023-06-08 16:45:20.388836, title: null}, {quantity: 1, price: null, company: Choose Company, model: KD-65X75L IN5, id: 2023-06-08 16:45:20.838656, title: null}, {quantity: 1, price: null, model: HM SAC RAS222PCAIBA, company: Choose Company, id: 2023-06-08 16:45:21.499394, title: null}]
现在我想使用 CartItem.fromJson 将其转换为 Cart Item 对象:
CartItem.fromJson(Map<String, Object?> json)
: this(
id: json['id'] as String?,
title: json['title'] as String?,
quantity: json['quantity'] as int?,
price: json['price'] as double?,
company: json['company'] as String?,
model: json['model'] as String?,
);
当我尝试打印 company 属性时,它显示以下错误:
I/flutter (12337): All Orders
I/flutter (12337): null
I/chatty (12337): uid=10091(com.example.ts) 1.ui identical 1 line
I/flutter (12337): null
英文:
I am reading the Firebase documents as below
collectionRef.get().then(
(querySnapshot) {
print("All Orders");
for (var docSnapshot in querySnapshot.docs) {
var _product = docSnapshot.data();
print(CartItem.fromJson(_product).company);
}
},
onError: (e) => print("Error completing: $e"),
);
The print statement is printing the value as List of object
[{quantity: 1, price: null, model: TP WM TWT95-P102GB, company: Choose Company, id: 2023-06-08 16:45:20.388836, title: null}, {quantity: 1, price: null, company: Choose Company, model: KD-65X75L IN5, id: 2023-06-08 16:45:20.838656, title: null}, {quantity: 1, price: null, model: HM SAC RAS222PCAIBA, company: Choose Company, id: 2023-06-08 16:45:21.499394, title: null}]
Now i want to convert it into Cart Item object using CartItem.fromJson
CartItem.fromJson(Map<String, Object?> json)
: this(
id: json['id'] as String?,
title: json['title'] as String?,
quantity: json['quantity'] as int?,
price: json['price'] as double?,
company: json['company'] as String?,
model: json['model'] as String?,
);
It is showing the below error when i am trying to print the company attribute
I/flutter (12337): All Orders
I/flutter (12337): null
I/chatty (12337): uid=10091(com.example.ts) 1.ui identical 1 line
I/flutter (12337): null
答案1
得分: 0
检查您的 _product
的数据输出,确保您拥有正确的“flat”数据,并且没有将购物车项目信息封装在对象中。
我个人会这样写我的工厂,如果你所有的字段都是可为空的。
factory CartItem.fromJson(Map<String, dynamic> json) {
return CartItem(
quantity: json['quantity'],
price: json['price']?.toDouble(),
model: json['model'],
company: json['company'],
id: json['id'],
title: json['title'],
);
}
在我这边运行良好,如果需要更多上下文/信息,请提供。
英文:
Check the data output of your _product
to make sure you have the correct 'flat' data and that there is no object encapsulating your cart item info.
I personally write my factory like that if all of your fields are nullable.
factory CartItem.fromJson(Map<String, dynamic> json) {
return CartItem(
quantity: json['quantity'],
price: json['price']?.toDouble(),
model: json['model'],
company: json['company'],
id: json['id'],
title: json['title'],
);
}
It works fine on my side with this samples please provide more context/info if needed.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论