[英]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.