gpt4 book ai didi

flutter - Dart 将 Map 转换为 Map>

转载 作者:行者123 更新时间:2023-12-01 23:01:20 24 4
gpt4 key购买 nike

如何转换这样的Map:

Map<dynamic, dynamic> a = {2022-04-10: [8:00, 15:00]}; //No quotes

获得:

Map<String, List<String>> b = {"2022-04-10": ["8:00", "15:00"]};

我试过这样做,但它对我不起作用。没有错误,但是如果你调用 print,什么也不会发生:

String str = '{2022-04-10: [8:00, 15:00]}';
final Map<dynamic, dynamic> dynamicMapFromJson = json.decode(str);
Map<String, List<String>> schedule = dynamicMapFromJson.cast<String, List<String>>();

我正在从 Firebase 教授这张 map :

enter image description here

await FirebaseFirestore.instance
.collection('call')
.get()
.then((QuerySnapshot querySnapshot) {
for (var doc in querySnapshot.docs) {
Map<String, List<String>> _schedule = doc.get('schedule');
}
});

最佳答案

你的 map 是

Map<dynamic, dynamic> a = {"2022-04-10": ["8:00", "15:00"]};

有两种转换方式

Map<dynamic, dynamic> a = {"2022-04-10": ["8:00", "15:00"]};
Map<String, List<String>> converted = {};

// 1st way:
for (var item in a.keys)
converted[item.toString()] = List<String>.from(a[item]);

// 2nd way:
for (var item in a.keys)
{
List<String> _new = [];
for (var newItem in List.from(a[item]))
_new.add(newItem.toString());
converted[item.toString()] = _new;
}

关于flutter - Dart 将 Map<dynamic, dynamic> 转换为 Map<String, List<String>>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71819757/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com