Home > front end >  How to access the String inside the List in Map<String, List<String>>?
How to access the String inside the List in Map<String, List<String>>?

Time:07-30

final Map<String, List<String>> todoDate = {
'today': ['code', 'read a book']};

I am trying to display the String value in the List.

Text(todoDate['today'[0], //ERROR!

CodePudding user response:

It is possible to get null data while reading map. And Text widget doesn't accept null value. You can provide default value on null cases.

Text(todoDate['today']?[0]??"default");

CodePudding user response:

Just use the null assertion operator ! to remove the null error.

It would be like this:

print(todoDate['today']![0]);

Just make sure the field exists otherwise you'd need to check if it's null first. And to check it you can use the null aware operator ? in conjunction with the null-coalescing operator ?? to default it to something else instead.

  • Related