enter image description here
I'm new to flutter and trying to use spread operator and Map into a List but I get null safety error , tried using (!) but no good.
CodePudding user response:
Although I don't quite understand what you are talking about, I roughly understand where your code is wrong.
This is the example code:
change
_snapshot.data!.name
to
_snapshot.data?.name ?? "Unknown"
CodePudding user response:
First, maybe to someone reading this you have already edited your question as asked in the comments by @nagendra nag and @CLucera. Your error should stop if you just change this:
(questions[indexNum]['answers'] as List<String>)
to this:
((questions[indexNum]['answers'] ?? []) as List<String>)
The error is about the 'answers' field being empty or null and you are trying to cast it as a non-nullable List
. I suggested the above instead of just casting it to List<String>?
because if you cast it this way, the map operation would have to be called on a nullable value (yourListCasted?.map(...)
) and that would mean that this could not be called, making the spread operator complain about the object not being a List
.