需要Dart初学者帮助


var a = [{'answers' : [{'text':'Cloud','score':10},],},];
main()
{
print(a[0]['answers']);
}

我想在'score'中打印数字10谁来帮我修复代码!!首先感谢!!

你的问题是正确的null-safety,它抱怨使用['answers']的值。原因是Map上的[]操作符返回一个可空类型,因为如果该元素不存在于Map中,结果可以是null

我在下面使用!向编译器保证,您确信该元素确实存在于Map中,因此它停止抱怨。但是,如果返回值是null:

,它将在运行时插入检查并使应用程序崩溃。
var a = [
{
'answers': [
{'text': 'Cloud', 'score': 10},
],
},
];
void main() {
print(a[0]['answers']![0]['score']); // 10
}

最新更新