【问题标题】:Trouble accessing the object's array inside the array and their size无法访问数组内的对象数组及其大小
【发布时间】: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'.

我该怎么做?另外,如果我只想打印“答案”的所有三个选项(“文本”),我该怎么办。我知道这是一个有点愚蠢的问题,但我完全被困在这里。任何帮助将不胜感激。

【问题讨论】:

    标签: arrays list object dart


    【解决方案1】:

    你可以使用这个技巧。

    var answers = questions[0]['answers'] as List<Map<String, Object>>;
    print('${answers.length}'); //3
    print('${answers[0]}'); //{text: Black, score: 3}
    

    或者一行。

    print('${(questions[0]['answers'] as List<Map<String, Object>>).length}'); //3
    

    【讨论】:

    • 这确实很有帮助。谢谢@suhaib
    【解决方案2】:

    通过您使用的符号,Dart 将类型推断为具有字符串键但混合值类型的映射列表(questionText 是字符串,answers 又是映射列表)。这迫使 Dart 使用 Object 作为值类型,这使得不强制转换就无法访问嵌套字段。

    questions[0].runtimeType // Type (_InternalLinkedHashMap<String, Object>)
    

    您还可以通过检查 questions 对象在调试器中轻松看到这一点。

    我建议定义具有正确类型属性的类。

    class Question {
      Question(this.text, this.answers);
    
      final String text;
    
      final List<Answer> answers;
    
      String toString() => 'Question { text: $text, answers: $answers }';
    }
    
    class Answer {
      Answer(this.text, this.score);
    
      final String text;
    
      final int score;
    
      String toString() => 'Answer { text: $text, score: $score }';
    }
    
    main(List<String> args) {
      final questions = [
        Question('What\'s your favorite color?', [
          Answer('Black', 3),
          Answer('Blue', 1),
          Answer('White', 1),
        ]),
        Question('What\'s your favorite animal?', [
          Answer('Lion', 3),
          Answer('Snake', 5),
          Answer('Cat', 1),
        ]),
        Question('What\'s your favorite teacher?', [
          Answer('Ali', 3),
          Answer('Aslam', 3),
          Answer('Akram', 3),
        ]),
      ];
    
      print(questions.length); // 3
      print(questions[0].text); // What's your favorite color?
      print(questions[0].answers); // [Answer { text: Black, score: 3 }, Answer { text: Blue, score: 1 }, Answer { text: White, score: 1 }]
      print(questions[0].answers.length); // 3
      questions[0].answers.forEach((answer) => print(answer.text)); // print all three options('text') of 'answers'
      print(questions[0].answers[0]); // Answer { text: Black, score: 3 }
      print(questions[0].answers[0].text); // Black
      print(questions[0].answers[0].score); // 3
    }
    

    【讨论】:

    • 非常感谢@Felix。这有很大帮助。这个小问题对我来说变得巨大。你是救世主。保持幸福
    猜你喜欢
    • 2019-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-13
    • 1970-01-01
    • 2018-11-22
    相关资源
    最近更新 更多