反序列化cloud_firestore数据时遇到问题。 (飞镖/扑打)

时间:2019-11-16 13:53:03

标签: json dart google-cloud-firestore json-deserialization

反序列化来自cloud_firestore数据库的一个非常复杂的JSON对象时遇到一些问题。我已经采取了一种相当漂亮的方式格式化该对象。所以这是它的样子:

{
 "mealType": "Breakfast",
 "id": 6,
 "totalCalories": 187.0,
 "nutritionixData": [
  {
   "phospate": 152.15,
   "totalFat": 11.11,
   "potassium": 173.4,
   "servingQuantity": 3.0,
   "photo": {
    "thumbnail": "https://d2xdmhkmkbyw75.cloudfront.net/9_thumb.jpg",
    "highRes": null
   },
   "totalCarbohydrate": 0.04,
   "calories": 187.0,
   "nutrients": [
    {
     "attributeID": 203,
     "value": 20.366
    },
    {
     "attributeID": 204,
     "value": 11.1095
    },
    {
     "attributeID": 205,
     "value": 0.0425
    },
    {
     "attributeID": 207,
     "value": 0.7225
    },
    {
     "attributeID": 208,
     "value": 187.0
    },
    {
     "attributeID": 255,
     "value": 52.9975
    },
    {
     "attributeID": 268,
     "value": 782.0
    },
    {
     "attributeID": 291,
     "value": 0.0
    },
    {
     "attributeID": 301,
     "value": 11.05
    },
    {
     "attributeID": 303,
     "value": 1.3685
    },
    {
     "attributeID": 304,
     "value": 17.0
    },
    {
     "attributeID": 305,
     "value": 152.15
    },
    {
     "attributeID": 306,
     "value": 173.4
    },
    {
     "attributeID": 307,
     "value": 60.35
    },
    {
     "attributeID": 309,
     "value": 1.4535
    },
    {
     "attributeID": 312,
     "value": 0.0604
    },
    {
     "attributeID": 315,
     "value": 0.023
    },
    {
     "attributeID": 317,
     "value": 13.345
    },
    {
     "attributeID": 318,
     "value": 507.45
    },
    {
     "attributeID": 319,
     "value": 152.15
    },
    {
     "attributeID": 320,
     "value": 152.15
    },
    {
     "attributeID": 401,
     "value": 0.34
    },
    {
     "attributeID": 404,
     "value": 0.0485
    },
    {
     "attributeID": 405,
     "value": 0.1607
    },
    {
     "attributeID": 406,
     "value": 5.9764
    },
    {
     "attributeID": 410,
     "value": 0.8526
    },
    {
     "attributeID": 415,
     "value": 0.289
    },
    {
     "attributeID": 417,
     "value": 19.55
    },
    {
     "attributeID": 418,
     "value": 0.663
    },
    {
     "attributeID": 431,
     "value": 0.0
    },
    {
     "attributeID": 432,
     "value": 19.55
    },
    {
     "attributeID": 435,
     "value": 19.55
    },
    {
     "attributeID": 501,
     "value": 0.2253
    },
    {
     "attributeID": 502,
     "value": 0.8449
    },
    {
     "attributeID": 503,
     "value": 1.009
    },
    {
     "attributeID": 504,
     "value": 1.4841
    },
    {
     "attributeID": 505,
     "value": 1.6363
    },
    {
     "attributeID": 506,
     "value": 0.5372
    },
    {
     "attributeID": 507,
     "value": 0.2729
    },
    {
     "attributeID": 508,
     "value": 0.7973
    },
    {
     "attributeID": 509,
     "value": 0.652
    },
    {
     "attributeID": 510,
     "value": 0.9911
    },
    {
     "attributeID": 511,
     "value": 1.2895
    },
    {
     "attributeID": 512,
     "value": 0.5848
    },
    {
     "attributeID": 513,
     "value": 1.1815
    },
    {
     "attributeID": 514,
     "value": 1.8224
    },
    {
     "attributeID": 515,
     "value": 2.9801
    },
    {
     "attributeID": 516,
     "value": 1.3413
    },
    {
     "attributeID": 517,
     "value": 1.0098
    },
    {
     "attributeID": 518,
     "value": 0.7327
    },
    {
     "attributeID": 601,
     "value": 79.9
    },
    {
     "attributeID": 606,
     "value": 3.111
    },
    {
     "attributeID": 610,
     "value": 0.0
    },
    {
     "attributeID": 611,
     "value": 0.017
    },
    {
     "attributeID": 612,
     "value": 0.085
    },
    {
     "attributeID": 613,
     "value": 2.278
    },
    {
     "attributeID": 614,
     "value": 0.629
    },
    {
     "attributeID": 617,
     "value": 3.655
    },
    {
     "attributeID": 618,
     "value": 2.108
    },
    {
     "attributeID": 619,
     "value": 0.0935
    },
    {
     "attributeID": 620,
     "value": 0.102
    },
    {
     "attributeID": 621,
     "value": 0.0255
    },
    {
     "attributeID": 626,
     "value": 0.612
    },
    {
     "attributeID": 628,
     "value": 0.1105
    },
    {
     "attributeID": 629,
     "value": 0.0085
    },
    {
     "attributeID": 630,
     "value": 0.0
    },
    {
     "attributeID": 631,
     "value": 0.017
    },
    {
     "attributeID": 645,
     "value": 4.437
    },
    {
     "attributeID": 646,
     "value": 2.4225
    }
   ],
   "saturatedFat": 3.11,
   "serving_weight": 85.0,
   "sodium": 60.35,
   "foodName": "chicken",
   "dietaryFiber": 0.0,
   "protein": 20.37,
   "cholesterol": 79.9,
   "sugar": 0.0
  }
 ],
 "meals": "chicken"
}

这是我尝试反序列化此对象的方法:

class FirebaseFood {
  int id;
  String mealType;
  String meals;
  List<FireNutritionix> fireNutritionix;
  double totalCalories;

  FirebaseFood(
      {this.fireNutritionix,
      this.id,
      this.meals,
      this.mealType,
      this.totalCalories});

  factory FirebaseFood.fromFirebase(Map<dynamic, dynamic> parsedFirebase) {
    var list = parsedFirebase['nutritionixData'] as List;
    List<FireNutritionix> fireNutritionix =
        list.map((i) => FireNutritionix.fromFirebase(i)).toList();

    return FirebaseFood(
        fireNutritionix: fireNutritionix,
        id: parsedFirebase['id'],
        meals: parsedFirebase['meals'],
        mealType: parsedFirebase['mealType'],
        totalCalories: parsedFirebase['totalCalories']);
  }
}

class FireNutritionix {
  double calories;
  double cholesterol;
  double dietaryFiber;
  List<FireNutrients> fireNutrients;
  double phospate;
  List<FirePhoto> photo;
  double potassium;
  double protein;
  double saturatedFat;
  double servingQuantity;
  double serving_weight;
  double sodium;
  double sugar;
  double totalCarbohydrate;
  double totalFat;

  FireNutritionix(
      {this.calories,
      this.cholesterol,
      this.dietaryFiber,
      this.fireNutrients,
      this.phospate,
      this.photo,
      this.potassium,
      this.protein,
      this.saturatedFat,
      this.serving_weight,
      this.servingQuantity,
      this.sodium,
      this.sugar,
      this.totalCarbohydrate,
      this.totalFat});

  factory FireNutritionix.fromFirebase(Map<dynamic, dynamic> parsedFirebase) {
      var list = parsedFirebase['nutrients'] as List; // being called on null
      List<FireNutrients> fireNutrients =
          list.map((i) => FireNutrients.fromFirebase(i)).toList();

      var list2 = parsedFirebase['photo'] as List; // being called on null
      List<FirePhoto> firePhoto = list2.map((i) => FirePhoto.fromFirebase(i));

    return FireNutritionix(
        calories: parsedFirebase['calories'],
        cholesterol: parsedFirebase['cholesterol'],
        dietaryFiber: parsedFirebase['dietaryFiber'],
        fireNutrients: fireNutrients,
        phospate: parsedFirebase['phospate'],
        photo: firePhoto,
        potassium: parsedFirebase['potassium'],
        protein: parsedFirebase['protein'],
        saturatedFat: parsedFirebase['saturatedFat'],
        serving_weight: parsedFirebase['serving_weight'],
        servingQuantity: parsedFirebase['servingQuantity'],
        sodium: parsedFirebase['sodium'],
        sugar: parsedFirebase['sugar'],
        totalCarbohydrate: parsedFirebase['totalCarbohydrate'],
        totalFat: parsedFirebase['totalFat']);
  }
}

class FireNutrients {
  int attributeID;
  double value;

  FireNutrients({this.attributeID, this.value});

  factory FireNutrients.fromFirebase(Map<dynamic, dynamic> parsedFirebase) {
    return FireNutrients(
        attributeID: parsedFirebase['attributeID'],
        value: parsedFirebase['value']);
  }
}

class FirePhoto {
  String highRes;
  String thumbnail;

  FirePhoto({this.highRes, this.thumbnail});

  factory FirePhoto.fromFirebase(Map<dynamic, dynamic> parsedFirebase) {
    return FirePhoto(
        highRes: parsedFirebase['highRes'],
        thumbnail: parsedFirebase['thumbnail']);
  }
}

我得到的错误是:

E/flutter ( 9185): Receiver: null
E/flutter ( 9185): Tried calling: map<FireNutrients>(Closure: (dynamic) => FireNutrients)
E/flutter ( 9185): #0      Object.noSuchMethod  (dart:core-patch/object_patch.dart:51:5)
E/flutter ( 9185): #1      new FireNutritionix.fromFirebase 
package:thesis/fromFirebase/firebaseFood.dart:69
E/flutter ( 9185): #2      _FitnessAppHomeScreenState.checkMealsList.<anonymous closure>.<anonymous closure> 
package:thesis/fitnessAppHomeScreen.dart:182
E/flutter ( 9185): #3      List.forEach  (dart:core-patch/growable_array.dart:283:8)

虽然我不明白为什么要对null调用parsedFirebase ['Nutritions']。.任何帮助和解释将不胜感激!非常感谢!

0 个答案:

没有答案