How to filter Map in Flutter?

5,009

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];
Share:
5,009
Nick
Author by

Nick

Updated on December 11, 2022

Comments

  • Nick
    Nick over 1 year

    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”];
    
  • Nick
    Nick almost 5 years
    didn't work. The method '[]' was called on null. And it pains middle of this [_amount]["MATURITY"]
  • Gazihan Alankus
    Gazihan Alankus almost 5 years
    I edited to create a debug code, could you see which line fails there?