Dart 将 Firebase 对象转换为 Pojo 类。

huangapple go评论66阅读模式
英文:

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(&quot;All Orders&quot;);
		       for (var docSnapshot in querySnapshot.docs) {
		        
		         var _product = docSnapshot.data();
		         print(CartItem.fromJson(_product).company);
		
		       }
		     },
		     onError: (e) =&gt; print(&quot;Error completing: $e&quot;),
		   );

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&lt;String, Object?&gt; json)
			    : this(
			        id: json[&#39;id&#39;] as String?,
			        title: json[&#39;title&#39;] as String?,
			        quantity: json[&#39;quantity&#39;] as int?,
			        price: json[&#39;price&#39;] as double?,
			        company: json[&#39;company&#39;] as String?,
			        model: json[&#39;model&#39;] 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&lt;String, dynamic&gt; json) {
    return CartItem(
      quantity: json[&#39;quantity&#39;],
      price: json[&#39;price&#39;]?.toDouble(),
      model: json[&#39;model&#39;],
      company: json[&#39;company&#39;],
      id: json[&#39;id&#39;],
      title: json[&#39;title&#39;],
    );
  }

It works fine on my side with this samples please provide more context/info if needed.

huangapple
  • 本文由 发表于 2023年6月8日 21:17:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76432267.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定