Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 如何在Dart中访问多维数组/列表中的元素_Arrays_Dart - Fatal编程技术网

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来确定它。请记住接受答案。