简体   繁体   中英

How can I convert List<Item> to Map<Datetime, List<Item>> in dart / Flutter

I have a list of difined Todo items like:

Todo{
 Datetime datetime;
 String task;
}

List 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:

todos.forEach((todo) => map2[todo.datetime] = todo.task); the next todo will be replaced if the key is the same.

So sorry if this is a silly question, I have searched on Google but nothing there. Thanks, Hung PT.

尝试这个

todos.forEach((todo) => map2[todo.datetime] = map2[todo.datetime] == null ? [...map2[todo.datetime], todo.task] : [todo.task]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM