简体   繁体   English

如何从 Map 转换字符串值<string, list<string> &gt; 键入 Double? Flutter</string,>

[英]How to Convert String Values From Map<String, List<String>> to type Double? Flutter

I have我有

Map<String, List<String>> myMap = {'A': ['A','B','C'], '1': ['1','2','3'], '2': ['4','5','6'] };

I would like to convert the numerical values in Lists '1' and '2' from String to type Double.我想将列表“1”和“2”中的数值从字符串转换为双精度类型。 This is what I want to achieve:这就是我想要实现的目标:

Map<String, List<String>> myMap = {'A': ['A','B','C'], '1': [1.00, 2.00, 3.00], '2': [4.00, 5.00, 6.00] };
void main() {
  Map<String, List<dynamic>> myMap = {
    'A': ['A', 'B', 'C'],
    '1': ['1', '2', '3'],
    '2': ['4', '5', '6']
  };

  myMap.forEach((key, value) {
    List<double> list = [];
    if (isNumeric(key)) {
      myMap[key].forEach((value) {
        list.add(double.parse(value));
      });
      myMap[key] = list;
    }
  });

  print(myMap);
  
}
//check if string value is numeric
bool isNumeric(String s) {
  if (s == null) {
    return false;
  }
  return double.parse(s, (e) => null) != null;
}

OUTPUT: OUTPUT:

{A: [A, B, C], 1: [1.0, 2.0, 3.0], 2: [4.0, 5.0, 6.0]}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 flutter 问题:如何将这种类型的double从string转换? - flutter problem : How to convert this type of double from string? Flutter 转换列表中的字符串 double - Flutter convert string in list double Flutter MethodChannel 嵌套值:&#39;列表<dynamic> &#39; 不是类型 &#39;FutureOr 的子类型<List<Map<String, double> &gt;&gt;&#39; - Flutter MethodChannel nested values: 'List<dynamic>' is not a subtype of type 'FutureOr<List<Map<String, double>>>' 如何在 flutter 中将字符串转换为 double? - how to convert a string to double in flutter? 如何转换列表<String>在颤振中输入 int 类型 - How to convert List<String> to int type in flutter 将列表从 api 转换为列表<map<string, dynamic> > 和清单<map<string, string> > 在 flutter </map<string,></map<string,> - Convert List from api to List<Map<String, dynamic>> and List<Map<String, String>> in flutter flutter 类型投到列表<map<string, string> &gt; </map<string,> - flutter type cast to List<Map<String, String>> 如何转换`List <map<string,string> &gt;` 到 `Set <map<string,string> &gt;` 在 flutter 中? </map<string,string></map<string,string> - How can I convert a `List<Map<String,String>>` to a `Set<Map<String,String>>` in flutter? 如何反转 Map 中的值<string, list<string> &gt; Flutter?</string,> - How to Reverse Values in Map <String, List<String>> Flutter? Flutter 如何转换列表<string>列出<custom type> ? 事件类型</custom></string> - Flutter How to convert List <string>to List<custom type>? Event type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM