2
votes

I have a json file as shown below. When user choose amount and maturity I need to show a maturity value.

Example: If user choose amount=500 and maturity= 24M. I need to show the number 6.

How to filter Map in Flutter ?

Here is what I did so far:

My json file:

{  
   “AMOUNT”:[  
      {  
         "500":{  
            “MATURITY":[  
               {  
                  "12M”:”4”
               },
               {  
                  "24M”:”6”
               },
               {  
                  "36M”:”8”
               },
               {  
                  "48M”:”10”
               },
               {  
                  "60M”:”12”
               }
            ]
         },
         “1000":{  
            “MATURITY":[  
               {  
                  "12M”:”8”
               },
               {  
                  "24M”:”12”
               },
               {  
                  "36M”:”16”
               },
               {  
                  "48M”:”20”
               },
               {  
                  "60M”:”24”
               }
            ]
         }
      }
   ]
}

Json Load:

// Test part trying to load json into basic map...
String jsonFastMaturity = await rootBundle.loadString("assets/myfile.json");
Map _mapFastMaturity = jsonDecode(jsonFastMaturity);
List _tmpFastMaturity = _mapFastMaturity[“AMOUNT”];
1

1 Answers

3
votes

This should work

   final amount = "500";
   final maturity = "24M";
   _mapFastMaturity["AMOUNT"][0][amount]["MATURITY"].singleWhere((m) => m.containsKey(maturity))[maturity]

This should help debug, if you are getting an error. Which line do you get an error here?

   final a = _mapFastMaturity["AMOUNT"];
   final b = a[0];
   final c = b[amount];
   final d = c["MATURITY"];
   final e = d.singleWhere((m) => m.containsKey(maturity));
   final f = e[maturity];