简体   繁体   中英

Mapping a list in flutter

I tried mapping a list of Question to a widget and it keep trowing back error when i run the app but no error warnings in the code. Please can someone help me detect what am doing wrong here.

...(questions[_questionIndex]['answer'] as List<String>).map((answer) {
              return Answer(_answerQuestion, answer);
            }).toList(),

from the code above am calling a question list with index of _questionIndex which is generated by the length of all the questions and then accessing a key with the name 'answer'. please the list is a list of maps of question and answers.

and here is my error in app:

════════ Exception caught by widgets library ═══════════════════════════════════
The following _CastError was thrown building MyApp(dirty, state: _MyAppState#38d02):
type 'Null' is not a subtype of type 'List<String>' in type cast

questions[_questionIndex]['answer'] contains a null value, and you're casting it to a non-nullable List<String> . Either ensure that the iterable does not contain any null values, or change your casting to List<String?> to allow for null values.

Thanks for all your contributions. I found the error it was a typo when i called the value from the Map.

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