【发布时间】:2022-01-06 15:20:38
【问题描述】:
我尝试将问题列表映射到小部件,但当我运行应用程序时它不断返回错误,但代码中没有错误警告。请有人帮我检测这里做错了什么。
...(questions[_questionIndex]['answer'] as List<String>).map((answer) {
return Answer(_answerQuestion, answer);
}).toList(),
从上面的代码中,我调用了一个索引为 _questionIndex 的问题列表,该索引由所有问题的长度生成,然后访问一个名为“answer”的键。请列表是问题和答案的地图列表。
这是我在应用程序中的错误:
════════ 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']的样本数据吗? -
"代码中没有错误警告" ... 不会出现任何静态分析错误,因为您通过执行显式
as List<String>强制转换显式抑制了它们。当你这样做时,你是在告诉分析器和编译器信任你。
标签: list flutter dart mobile mapping