我只想打印的分数值
void main() {
List questions = [
{
'questionText': 'What's your favorite color?',
'answers': [
{'text': 'Black', 'score': 10},
{'text': 'Red', 'score': 0},
{'text': 'Green', 'score': 0},
{'text': 'White', 'score': 0},
],
},
];
我试着做
print(questions[0]["score"])
但它不起作用。有人能帮我吗
当您访问questions[0]
时,您将获得数组中的第一个元素,在本例中为:
{
'questionText': 'What's your favorite color?',
'answers': [
{'text': 'Black', 'score': 10},
{'text': 'Red', 'score': 0},
{'text': 'Green', 'score': 0},
{'text': 'White', 'score': 0},
],
}
当您编写questions[0]["score"]
时,您正试图获得密钥score
,正如您所看到的,它是null
。
您应该访问对象内部的answers
,看看下面的例子:
print(questions[0]["score"]); // null
print(questions[0]['answers'][0]['score']); // 10
print(questions[0]['answers'].map((answer) => answer['score'])); // (10, 0, 0, 0)