skip to Main Content

So what I am trying to do is that I am trying to call the API and upon the successful call of the API I want to insert the response in the local db and then later query the db and show the data in the UI.

Below is the response I get:

{
    "meta": [
        {
            "General": [
                {
                    "Name before Release": "BL 3264"
                },
                {
                    "Released Year": "2010"
                },
                {
                    "Recommended Areas": "Up to the height of 500 meter of sea level terai,plataeu and valley"
                }
            ]
        },
        {
            "Morphological Characteristics": [
                {
                    "Color of Seed": "Amber"
                },
                {
                    "Glaucosity of Flag Leaf Sheath": "Medium"
                },
                {
                    "Pinnacle Type": "Parallel"
                },
                {
                    "Presence of Purple Colour in Outer Auricle of Flagged-Leaf": "Medium"
                },
                {
                    "Ripen Pinnacle Colour": "White"
                },
                {
                    "Shape of Seed": "Long"
                },
                {
                    "Shine of Pennicle of Wheat (Ear Glaucosity)": "Medium"
                }
            ]
        },
        {
            "Agronomical Characteristics": [
                {
                    "Average Height (cm)": "96"
                },
                {
                    "Maturing Days (days)": "118"
                },
                {
                    "Pinacle days (days)": "75"
                },
                {
                    "Quantity of tiler": "255"
                },
                {
                    "Resistant Characteristics": "Can resist yellow and vermillion leaf disease, can withstand hot air which flows after  the formation of tiller, can yield in high amount even in the soil where less amount of boron is found "
                },
                {
                    "Weight of Thousand grains (gram)": "45"
                },
                {
                    "Yield (ton/ha)": "4.79"
                }
            ]
        },
        {
            "Nutritional and Other Qualities": [
                {
                    "Percentage of Protein (%)": "11.97"
                }
            ]
        },
        {
            "Special Identifying Characteristics": [
                {
                    "Special Description": "1.Oily weighty paniclen2.Big white color strong seedn3.Ripen faster than other species "
                }
            ]
        },
        {
            "imagearray": [
                {
                    "Aditya_1_6YWkYQd": "https://sqccapi.pathway.com.np/media/CropVarietyImages/1_6YWkYQd.JPG"
                },
                {
                    "Aditya_Capture_2_LuAqweK": "https://sqccapi.pathway.com.np/media/CropVarietyImages/Capture_2_LuAqweK.JPG"
                },
                {
                    "Aditya_Capture3_dsU6RNl": "https://sqccapi.pathway.com.np/media/CropVarietyImages/Capture3_dsU6RNl.JPG"
                },
                {
                    "Aditya_Capture4_vgvD6R0": "https://sqccapi.pathway.com.np/media/CropVarietyImages/Capture4_vgvD6R0.JPG"
                }
            ]
        }
    ],
         
}

And I tried it by calling the API and upon success, I inserted the data in the local db but it seemed like only the ‘meta’ was inserted in the db and then when i tried to display the data in the UI I only got Name before Release and BL 3264.
Following is the response I get.

2

Answers


  1. You can do something like this general:json[‘meta’][‘General’] while parsing json and vice versa for all the properties of meta to access them.

    Login or Signup to reply.
  2. I used a Json Helper tool: https://app.quicktype.io/ , it can be cleaned up, but is getting all the data into the logical objects, after that, you can use a NOSQL database like https://pub.dev/packages/objectbox

    Future<void> readJson() async {
        final String response = await rootBundle.loadString('assets/example.json');
        final data = await json.decode(response);
        final MetaData metaData = MetaData.fromJson(data);
        //print(metaData);
       
        final box = store.box<MetaData >();
       // Put a new object into the box
    
        final id = box.put(metaData);
    
       // Get the object back from the box
        final newMeta = box.get(id)!;   
    }
    

    The objects:

    class MetaData {
      Meta meta;
    
      MetaData({
        required this.meta,
      });
    
      factory MetaData.fromRawJson(String str) =>
          MetaData.fromJson(json.decode(str));
    
      String toRawJson() => json.encode(toJson());
    
      factory MetaData.fromJson(Map<String, dynamic> json) => MetaData(
            meta: Meta.fromList(json["meta"]),
          );
    
      Map<String, dynamic> toJson() => {
            "meta": meta.toJson(),
          };
    }
    
    class Meta {
      General general;
      MorphologicalCharacteristic morphologicalCharacteristics;
      AgronomicalCharacteristic agronomicalCharacteristics;
      NutritionalAndOtherQuality nutritionalAndOtherQualities;
      SpecialIdentifyingCharacteristic specialIdentifyingCharacteristics;
      Imagearray imagearray;
    
      Meta({
        required this.general,
        required this.morphologicalCharacteristics,
        required this.agronomicalCharacteristics,
        required this.nutritionalAndOtherQualities,
        required this.specialIdentifyingCharacteristics,
        required this.imagearray,
      });
    
      factory Meta.fromRawJson(String str) => Meta.fromJson(json.decode(str));
    
      String toRawJson() => json.encode(toJson());
    
      factory Meta.fromJson(Map<String, dynamic> json) => Meta(
            general: General.fromList(json["General"]),
            morphologicalCharacteristics: MorphologicalCharacteristic.fromList(
                json["Morphological Characteristics"]),
            agronomicalCharacteristics: AgronomicalCharacteristic.fromList(
                json["Agronomical Characteristics"]),
            nutritionalAndOtherQualities: NutritionalAndOtherQuality.fromList(
                json["Nutritional and Other Qualities"]),
            specialIdentifyingCharacteristics:
                SpecialIdentifyingCharacteristic.fromList(
                    json["Special Identifying Characteristics"]),
            imagearray: Imagearray.fromList(json["imagearray"]),
          );
    
      factory Meta.fromList(List<dynamic> json) => Meta(
            general: General.fromList(json[0]["General"]),
            morphologicalCharacteristics: MorphologicalCharacteristic.fromList(
                json[1]["Morphological Characteristics"]),
            agronomicalCharacteristics: AgronomicalCharacteristic.fromList(
                json[2]["Agronomical Characteristics"]),
            nutritionalAndOtherQualities: NutritionalAndOtherQuality.fromList(
                json[3]["Nutritional and Other Qualities"]),
            specialIdentifyingCharacteristics:
                SpecialIdentifyingCharacteristic.fromList(
                    json[4]["Special Identifying Characteristics"]),
            imagearray: Imagearray.fromList(json[5]["imagearray"]),
          );
    
      Map<String, dynamic> toJson() => {
            "General": general.toJson(),
            "Morphological Characteristics": morphologicalCharacteristics.toJson(),
            "Agronomical Characteristics": agronomicalCharacteristics.toJson(),
            "Nutritional and Other Qualities":
                nutritionalAndOtherQualities.toJson(),
            "Special Identifying Characteristics":
                specialIdentifyingCharacteristics.toJson(),
            "imagearray": imagearray.toJson(),
          };
    }
    
    class AgronomicalCharacteristic {
      String averageHeightCm;
      String maturingDaysDays;
      String pinacleDaysDays;
      String quantityOfTiler;
      String resistantCharacteristics;
      String weightOfThousandGrainsGram;
      String yieldTonHa;
    
      AgronomicalCharacteristic({
        required this.averageHeightCm,
        required this.maturingDaysDays,
        required this.pinacleDaysDays,
        required this.quantityOfTiler,
        required this.resistantCharacteristics,
        required this.weightOfThousandGrainsGram,
        required this.yieldTonHa,
      });
    
      factory AgronomicalCharacteristic.fromRawJson(String str) =>
          AgronomicalCharacteristic.fromJson(json.decode(str));
    
      String toRawJson() => json.encode(toJson());
    
      factory AgronomicalCharacteristic.fromJson(Map<String, dynamic> json) =>
          AgronomicalCharacteristic(
            averageHeightCm: json["Average Height (cm)"],
            maturingDaysDays: json["Maturing Days (days)"],
            pinacleDaysDays: json["Pinacle days (days)"],
            quantityOfTiler: json["Quantity of tiler"],
            resistantCharacteristics: json["Resistant Characteristics"],
            weightOfThousandGrainsGram: json["Weight of Thousand grains (gram)"],
            yieldTonHa: json["Yield (ton/ha)"],
          );
    
      factory AgronomicalCharacteristic.fromList(List<dynamic> json) =>
          AgronomicalCharacteristic(
            averageHeightCm: json[0]["Average Height (cm)"],
            maturingDaysDays: json[1]["Maturing Days (days)"],
            pinacleDaysDays: json[2]["Pinacle days (days)"],
            quantityOfTiler: json[3]["Quantity of tiler"],
            resistantCharacteristics: json[4]["Resistant Characteristics"],
            weightOfThousandGrainsGram: json[5]["Weight of Thousand grains (gram)"],
            yieldTonHa: json[6]["Yield (ton/ha)"],
          );
    
      Map<String, dynamic> toJson() => {
            "Average Height (cm)": averageHeightCm,
            "Maturing Days (days)": maturingDaysDays,
            "Pinacle days (days)": pinacleDaysDays,
            "Quantity of tiler": quantityOfTiler,
            "Resistant Characteristics": resistantCharacteristics,
            "Weight of Thousand grains (gram)": weightOfThousandGrainsGram,
            "Yield (ton/ha)": yieldTonHa,
          };
    }
    
    class General {
      String nameBeforeRelease;
      String releasedYear;
      String recommendedAreas;
    
      General({
        required this.nameBeforeRelease,
        required this.releasedYear,
        required this.recommendedAreas,
      });
    
      factory General.fromRawJson(String str) => General.fromJson(json.decode(str));
    
      String toRawJson() => json.encode(toJson());
    
      factory General.fromJson(Map<String, dynamic> json) => General(
            nameBeforeRelease: json["Name before Release"],
            releasedYear: json["Released Year"],
            recommendedAreas: json["Recommended Areas"],
          );
    
      factory General.fromList(List<dynamic> json) => General(
            nameBeforeRelease: json[0]["Name before Release"],
            releasedYear: json[1]["Released Year"],
            recommendedAreas: json[2]["Recommended Areas"],
          );
    
      Map<String, dynamic> toJson() => {
            "Name before Release": nameBeforeRelease,
            "Released Year": releasedYear,
            "Recommended Areas": recommendedAreas,
          };
    }
    
    class Imagearray {
      String aditya16YWkYQd;
      String adityaCapture2LuAqweK;
      String adityaCapture3DsU6RNl;
      String adityaCapture4VgvD6R0;
    
      Imagearray({
        required this.aditya16YWkYQd,
        required this.adityaCapture2LuAqweK,
        required this.adityaCapture3DsU6RNl,
        required this.adityaCapture4VgvD6R0,
      });
    
      factory Imagearray.fromRawJson(String str) =>
          Imagearray.fromJson(json.decode(str));
    
      String toRawJson() => json.encode(toJson());
    
      factory Imagearray.fromJson(Map<String, dynamic> json) => Imagearray(
            aditya16YWkYQd: json["Aditya_1_6YWkYQd"],
            adityaCapture2LuAqweK: json["Aditya_Capture_2_LuAqweK"],
            adityaCapture3DsU6RNl: json["Aditya_Capture3_dsU6RNl"],
            adityaCapture4VgvD6R0: json["Aditya_Capture4_vgvD6R0"],
          );
    
      factory Imagearray.fromList(List<dynamic> json) => Imagearray(
            aditya16YWkYQd: json[0]["Aditya_1_6YWkYQd"],
            adityaCapture2LuAqweK: json[1]["Aditya_Capture_2_LuAqweK"],
            adityaCapture3DsU6RNl: json[2]["Aditya_Capture3_dsU6RNl"],
            adityaCapture4VgvD6R0: json[3]["Aditya_Capture4_vgvD6R0"],
          );
    
      Map<String, dynamic> toJson() => {
            "Aditya_1_6YWkYQd": aditya16YWkYQd,
            "Aditya_Capture_2_LuAqweK": adityaCapture2LuAqweK,
            "Aditya_Capture3_dsU6RNl": adityaCapture3DsU6RNl,
            "Aditya_Capture4_vgvD6R0": adityaCapture4VgvD6R0,
          };
    }
    
    class MorphologicalCharacteristic {
      String colorOfSeed;
      String glaucosityOfFlagLeafSheath;
      String pinnacleType;
      String presenceOfPurpleColourInOuterAuricleOfFlaggedLeaf;
      String ripenPinnacleColour;
      String shapeOfSeed;
      String shineOfPennicleOfWheatEarGlaucosity;
    
      MorphologicalCharacteristic({
        required this.colorOfSeed,
        required this.glaucosityOfFlagLeafSheath,
        required this.pinnacleType,
        required this.presenceOfPurpleColourInOuterAuricleOfFlaggedLeaf,
        required this.ripenPinnacleColour,
        required this.shapeOfSeed,
        required this.shineOfPennicleOfWheatEarGlaucosity,
      });
    
      factory MorphologicalCharacteristic.fromRawJson(String str) =>
          MorphologicalCharacteristic.fromJson(json.decode(str));
    
      String toRawJson() => json.encode(toJson());
    
      factory MorphologicalCharacteristic.fromJson(Map<String, dynamic> json) =>
          MorphologicalCharacteristic(
            colorOfSeed: json["Color of Seed"],
            glaucosityOfFlagLeafSheath: json["Glaucosity of Flag Leaf Sheath"],
            pinnacleType: json["Pinnacle Type"],
            presenceOfPurpleColourInOuterAuricleOfFlaggedLeaf:
                json["Presence of Purple Colour in Outer Auricle of Flagged-Leaf"],
            ripenPinnacleColour: json["Ripen Pinnacle Colour"],
            shapeOfSeed: json["Shape of Seed"],
            shineOfPennicleOfWheatEarGlaucosity:
                json["Shine of Pennicle of Wheat (Ear Glaucosity)"],
          );
    
      factory MorphologicalCharacteristic.fromList(List<dynamic> json) =>
          MorphologicalCharacteristic(
            colorOfSeed: json[0]["Color of Seed"],
            glaucosityOfFlagLeafSheath: json[1]["Glaucosity of Flag Leaf Sheath"],
            pinnacleType: json[2]["Pinnacle Type"],
            presenceOfPurpleColourInOuterAuricleOfFlaggedLeaf: json[3]
                ["Presence of Purple Colour in Outer Auricle of Flagged-Leaf"],
            ripenPinnacleColour: json[4]["Ripen Pinnacle Colour"],
            shapeOfSeed: json[5]["Shape of Seed"],
            shineOfPennicleOfWheatEarGlaucosity: json[6]
                ["Shine of Pennicle of Wheat (Ear Glaucosity)"],
          );
    
      Map<String, dynamic> toJson() => {
            "Color of Seed": colorOfSeed,
            "Glaucosity of Flag Leaf Sheath": glaucosityOfFlagLeafSheath,
            "Pinnacle Type": pinnacleType,
            "Presence of Purple Colour in Outer Auricle of Flagged-Leaf":
                presenceOfPurpleColourInOuterAuricleOfFlaggedLeaf,
            "Ripen Pinnacle Colour": ripenPinnacleColour,
            "Shape of Seed": shapeOfSeed,
            "Shine of Pennicle of Wheat (Ear Glaucosity)":
                shineOfPennicleOfWheatEarGlaucosity,
          };
    }
    
    class NutritionalAndOtherQuality {
      String percentageOfProtein;
    
      NutritionalAndOtherQuality({
        required this.percentageOfProtein,
      });
    
      factory NutritionalAndOtherQuality.fromRawJson(String str) =>
          NutritionalAndOtherQuality.fromJson(json.decode(str));
    
      String toRawJson() => json.encode(toJson());
    
      factory NutritionalAndOtherQuality.fromJson(Map<String, dynamic> json) =>
          NutritionalAndOtherQuality(
            percentageOfProtein: json["Percentage of Protein (%)"],
          );
    
      factory NutritionalAndOtherQuality.fromList(List<dynamic> json) =>
          NutritionalAndOtherQuality(
            percentageOfProtein: json[0]["Percentage of Protein (%)"],
          );
    
      Map<String, dynamic> toJson() => {
            "Percentage of Protein (%)": percentageOfProtein,
          };
    }
    
    class SpecialIdentifyingCharacteristic {
      String specialDescription;
    
      SpecialIdentifyingCharacteristic({
        required this.specialDescription,
      });
    
      factory SpecialIdentifyingCharacteristic.fromRawJson(String str) =>
          SpecialIdentifyingCharacteristic.fromJson(json.decode(str));
    
      String toRawJson() => json.encode(toJson());
    
      factory SpecialIdentifyingCharacteristic.fromJson(
              Map<String, dynamic> json) =>
          SpecialIdentifyingCharacteristic(
            specialDescription: json["Special Description"],
          );
      factory SpecialIdentifyingCharacteristic.fromList(List<dynamic> json) =>
          SpecialIdentifyingCharacteristic(
            specialDescription: json[0]["Special Description"],
          );
    
      Map<String, dynamic> toJson() => {
            "Special Description": specialDescription,
          };
    }
    

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search