我想更新地图内的数组
Map<int, List<String>> testtable = {
1: ['a', 'b', 'c', 'e'],
2: ['f', 'g', 'h', 'j'],
};
我想在 key = 1 的数组中更新 e => d,
我试过这个 testtable.update(1, (list[3]) => d);
但没有用
答案 0 :(得分:1)
你可以这样做:
testtable.update(
1,
(value) {
value[3] = 'd';
return value;
},
);
答案 1 :(得分:1)
update
期望新值作为参数,因此您必须返回整个列表。
testtable.update(1, (list) {
list[3] = 'd';
return list;
});
答案 2 :(得分:1)
另一种选择:
void main() {
Map<int, List<String>> testtable = {
1: ['a', 'b', 'c', 'e'],
2: ['f', 'g', 'h', 'j'],
};
print (testtable);
testtable[1][3]='d';
print (testtable);
}
答案 3 :(得分:1)
更新嵌套地图
Map<String, Map> selectedTimes = {
"monday": {"open": "7:30 AM", "close": "10:30 AM"},
"tuesday": {"open": "7:30 AM", "close": "10:30 AM"},
"wednesday": {"open": "7:30 AM", "close": "10:30 AM"},
"thursday": {"open": "7:30 AM", "close": "10:30 AM"},
"friday": {"open": "7:30 AM", "close": "10:30 AM"},
"saturday": {"open": "7:30 AM", "close": "10:30 AM"},
"sunday": {"open": "7:30 AM", "close": "10:30 AM"}
};
selectedTimes.update(day, (list) {
list.update(
time, (value) => selectedTimeRTL!.format(context).toString());
return list;
})