[英]How can I convert List<Item> to Map<Datetime, List<Item>> in dart / Flutter
I have a list of difined Todo items like:我有一个定义的 Todo 项目列表,例如:
Todo{
Datetime datetime;
String task;
}
List todos = [todo1, todo2,...]列出 todos = [todo1, todo2,...]
And I need to convert it to a map with type Map<Datetime, List<String>>
, but in todos list, I have some todo with the same Datetime so when I use code like:我需要将它转换为类型为Map<Datetime, List<String>>
,但在待办事项列表中,我有一些具有相同日期时间的待办事项,因此当我使用如下代码时:
todos.forEach((todo) => map2[todo.datetime] = todo.task);
the next todo will be replaced if the key is the same.如果 key 相同,则下一个 todo 将被替换。
So sorry if this is a silly question, I have searched on Google but nothing there.很抱歉,如果这是一个愚蠢的问题,我已经在谷歌上搜索过,但没有。 Thanks, Hung PT.谢谢,洪 PT。
尝试这个
todos.forEach((todo) => map2[todo.datetime] = map2[todo.datetime] == null ? [...map2[todo.datetime], todo.task] : [todo.task]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.