【问题标题】:How to filter Map in Flutter?如何在 Flutter 中过滤地图?
【发布时间】:2019-10-01 05:28:57
【问题描述】:

我有一个 json 文件,如下所示。当用户选择金额和期限时,我需要显示一个期限值。

示例:如果用户选择金额=500,期限=24M。我需要显示数字 6。

如何在 Flutter 中过滤 Map?

这是我到目前为止所做的:

我的 json 文件:

{  
   “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 加载:

// 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”];

【问题讨论】:

    标签: json filter dart flutter


    【解决方案1】:

    这应该可行

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

    如果您遇到错误,这应该有助于调试。你在哪一行得到了错误?

       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];
    

    【讨论】:

    • 没用。在 null 上调用了方法“[]”。它在这个 [_amount]["MATURITY"]
    • 我编辑创建了一个调试代码,你能看到哪一行出错了吗?
    猜你喜欢
    • 2020-11-28
    • 1970-01-01
    • 2021-12-12
    • 1970-01-01
    • 2021-10-29
    • 2020-10-10
    • 2021-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多