1
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!

2 Answers 2

1

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");
Sign up to request clarification or add additional context in comments.

3 Comments

Glad to help, Also beware of using !, without checking null. else it will raise on issue on null cases. You can test changing 'today' with something else.
could you explain your code please? Thanks
Here if we encounter null value from todoDate['today']?[0] using ?? will return right part default, Check this section
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.