Arrays 如何在Dart中访问多维数组/列表中的元素
我是飞镖初学者。 下面是一个数组,我需要访问一个特定的元素Arrays 如何在Dart中访问多维数组/列表中的元素,arrays,dart,Arrays,Dart,我是飞镖初学者。 下面是一个数组,我需要访问一个特定的元素 'text':'Rabbit'和'score':3 我尝试了以下方法: _questions[1]['answers'][0]['text']['score'] 这不管用 var _questions = [ { 'question': 'What is your color?', 'answers': [ {'text': 'Black', 'score': 10}, {'text': 'Red', 'sc
'text':'Rabbit'
和'score':3
我尝试了以下方法:
_questions[1]['answers'][0]['text']['score']
这不管用
var _questions = [
{
'question': 'What is your color?',
'answers': [
{'text': 'Black', 'score': 10},
{'text': 'Red', 'score': 5},
{'text': 'Green', 'score': 3},
{'text': 'White', 'score': 1},
],
},
{
'question': 'What is your animal?',
'answers': [
{'text': 'Rabbit', 'score': 3},
{'text': 'Snake', 'score': 11},
{'text': 'Elephant', 'score': 5},
{'text': 'Lion', 'score': 9},
],
},
];
(免责声明:我不知道Dart!)我使用了以下代码:
将应答对象强制转换为列表
:
var answers = (_questions[1]['answers'] as List);
然后您可以将第一个对象数据转换为:
answers[0]['text']
answers[0]['score']
这不管用。它给出了一个错误:没有为类“Object”定义运算符“[]”。谢谢Luis。这很有效。。。这意味着孩子不能直接接触。太好了。也许它可以访问,但我不知道足够的Dart来确定它。请记住接受答案。