【发布时间】:2020-03-20 02:09:06
【问题描述】:
我有一个简单的问题。请有人帮忙。
我是 Flutter 的绝对初学者。我无法访问列表中的对象及其大小。这是我想要达到的目标的一个简单示例。
final questions = [
{
'questionText': 'What's your favorite color?',
'answers': [
{'text': 'Black', 'score': 3},
{'text': 'Blue', 'score': 1},
{'text': 'White', 'score': 1}
]
},
{
'questionText': 'What's your favorite animal?',
'answers': [
{'text': 'Lion', 'score': 3},
{'text': 'Snake', 'score': 5},
{'text': 'Cat', 'score': 1}
]
},
{
'questionText': 'What's your favorite teacher?',
'answers': [
{'text': 'Ali', 'score': 3},
{'text': 'Aslam', 'score': 3},
{'text': 'Akram', 'score': 3}
]
}
];
print(questions.length); // 3
print(questions[0].length); // 2
print(questions[0]['answers']); //[{text: Black, score: 3}, {text: Blue, score: 1}, {text: White, score: 1}]
但是当我尝试通过以下方法获得问题中“答案”的长度时。我收到此错误(在 cmets 内部给出)。
print(questions[0]['answers'].length); //Error: The getter 'length' isn't defined for the class 'Object'.
当我尝试在问题中打印 'answers' 数组的第一个元素(即“{'text': 'Black', 'score': 3}”)时,我得到了以下错误
print(questions[0]['answers'][0]); // The method '[]' isn't defined for the class 'Object'
也试过这些
print(questions[0]['answers']['text']); // Error: The method '[]' isn't defined for the class 'Object'.
print(questions[0]['answers'[0]]); // null
print(questions[0]['answers'['text']]); // Error: The argument type 'String' can't be assigned //to the parameter type 'int'.
我该怎么做?另外,如果我只想打印“答案”的所有三个选项(“文本”),我该怎么办。我知道这是一个有点愚蠢的问题,但我完全被困在这里。任何帮助将不胜感激。
【问题讨论】: