Dictionary 如何更新将列表作为值的地图
我有一张Dictionary 如何更新将列表作为值的地图,dictionary,dart,types,Dictionary,Dart,Types,我有一张map类型的地图。其实例如下: 2020-11-26: [Test1, Test2, Test3, Test4] 2020-11-22: [Test331, Test55, Test1, Test55] 如何在列表值的一个位置更新这些键的值?这就是我尝试过的: \u myMap.update(\u day,(value)=>\u mapFieldController.text.toString()); 但目前它给出了以下错误: 错误:根据闭包上下文的要求,返回类型“String”不是
map
类型的地图。其实例如下:
2020-11-26: [Test1, Test2, Test3, Test4]
2020-11-22: [Test331, Test55, Test1, Test55]
如何在列表值的一个位置更新这些键的值?这就是我尝试过的:
\u myMap.update(\u day,(value)=>\u mapFieldController.text.toString());
但目前它给出了以下错误:
错误:根据闭包上下文的要求,返回类型“String”不是“List”
如何执行该操作,以及如何设置要更新的列表的位置?在这种情况下,我只想更新用户触摸的值。使用
\u myMap[\u day][0]=Test44444;
全样本:
void main(){
DateTime _day=DateTime.now();
映射_myMap={
_日期:['Test1','Test2','Test3','Test4'],
_day.add(持续时间(秒:5)):['Test331','Test55','Test1','Test55'],
};
打印(_myMap);
_myMap[_day][0]=“Test44444”;
打印(_myMap);
}
输出:
{2020-11-2514:03:07.880:[1,2,3,4],2020-11-2514:03:12.880:[2,3,5,6]}
{2020-11-25 14:03:07.880: [4444, 2, 3, 4], 2020-11-25 14:03:12.880: [2, 3, 5, 6]}
您可以做的一个示例:
//the map with some random data
Map<int, List<dynamic>> map = {
2020: ['test1', 'test2'],
1: ['m', 'a', 'p'],
};
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: ListView.builder(
itemCount: map.length,
itemBuilder: (context, mapIndex) {
//getting the current list with the current Index
List currentList = map.values.toList()[mapIndex];
return Column(
children: List.generate(currentList.length, (listIndex) {
return GestureDetector(
onTap: () {
//updating the "tapped" value
currentList[listIndex] = 'newValue';
setState(() {});
},
child: Text(currentList[listIndex]),
);
}),
);
},
),
),
);
}
//包含一些随机数据的地图
地图={
2020年:['test1','test2'],
1:['m','a','p'],
};
@凌驾
小部件构建(构建上下文){
返回脚手架(
正文:中(
子项:ListView.builder(
itemCount:map.length,
itemBuilder:(上下文,mapIndex){
//使用当前索引获取当前列表
List currentList=map.values.toList()[mapIndex];
返回列(
子项:List.generate(currentList.length,(listIndex){
返回手势检测器(
onTap:(){
//更新“抽头”值
currentList[listIndex]=“newValue”;
setState((){});
},
子项:文本(currentList[listIndex]),
);
}),
);
},
),
),
);
}
您可能希望限定Test1和Test2之类的内容是否实际上是字符串,或者是对代码段中未定义的对象的引用。只是字符串,但如果有两个相等的,我必须只识别selected@ChristopherMoore,这是我尝试过的,但没有成功。我的问题有待阅读。我知道错误很明显,但我不知道如何做我想做的(写在问题中),所以,你可以告诉我们你的答案。如果你知道,我不知道你的预期结果是什么。您必须提供更多详细信息才能获得完整答案。我想更新一些值,例如,第一个映射中的值0。。该值现在将为“test444”。所以,现在我的地图是2020-11-26:[Test44444,Test2,Test3,Test4]@TomBrady0fCode如果你尝试这个,它会更新值。如果这个更新,我会有另一个问题,因为这里不是。我将check@TomBrady0fCode我提供了一个完整的示例,说明这会更新值。这不是添加操作。