【问题标题】:How can I resolve " The argument type 'Object?' can't be assigned to the parameter type 'String' "?如何解决“参数类型‘对象’?不能分配给参数类型'String'”?
【发布时间】:2021-10-18 11:34:02
【问题描述】:

这就是我定义我的列表的方式:

final   _question = [
    {
      'questionText': ' what\'s your favourite color?',
      'answers': [
        {'text': 'Black', 'score': '10'},
        {'text': 'Green', 'score': '80'},
        {'text': 'Blue', 'score': '147'},
        {'text': 'Yellow', 'score': '60'},
      ]
    },
    {
      'questionText': 'what\'s your favourite animal?',
      'answers': [
        {'text': 'Rabbit', 'score': '10'},
        {'text': 'Tiger', 'score': '10'},
        {'text': 'Elephant', 'score': '10'},
        {'text': 'Lion', 'score': '10'},
      ]
    },
];

然后我调用列表,通过将地图转换为列表来提取“文本”和“分数”

Question(question[questionindex]['questionText']),
...(question[questionindex]['answers'] as List<Map<String,Object>>).map((answer) {
  return Answer(() => answerQuestion(answer['Score']), answer['text']);
}).toList(),

但我得到了这个错误“参数类型'对象?'不能分配给参数类型'String'"。

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    为什么不把它改成动态的,还要注意score是小写的s

    Question(question[questionindex]['questionText']),
    ...(question[questionindex]['answers'] as List<Map<String,dynamic>>).map((answer) {
      return Answer(() => answerQuestion(int.parse(answer['score'].toString())), answer['text'].toString());
    }).toList(),
    

    【讨论】:

    • 运行应用程序时出现此错误 ════════ 手势捕获异常══════════════════════ ═════════════════════类型'String'不是'score'类型'int'的子类型
    • 我编辑了我的答案,您需要使用 int.parse。因为您将分数作为字符串获取。答案消除了您的第一个错误,立即尝试,int 子类型错误也应该消失。
    【解决方案2】:

    你可以简单地将它转换成一个列表:

    final Map currentQuestion = question[currentQuestion];
    Question(currentQuestion['questionText'] as String),
    ...(currentQuestion['answers'] as List).map((answer) {
      return Answer(() => answerQuestion(answer['score'] as String), answer['text'] as String);
    }).toList(),
    

    我不知道您是否从服务器检索此数据为 JSON,但为了避免这些不必要的转换,只需将这些内部映射转换为类。你也可以看看 Dart 提供的this tutorial,它允许你使用jsonDecode 来转换地图。

    class Answer {
      final int score;
      final String text;
    
      const Answer(this.score, this.text);
    }
    
    class Question {
      final String text;
      final List<Answer> answers;
    
      const Question({required this.text, required this.answers});
    }
    

    这样声明你的问题:

    const List<Question> questions = [
      Question(text: "Who's Joe?", answers: [
        Answer(0, "Mama"), Answer(100, "Biden"), Answer(20, "Doe")
      ],
      Question(text: "What's Ligma?", answers: [
        Answer(0, "Balls"), Answer(20, "Nuts"), Answer(100, "Sigma"),
      ],
    ];
    

    然后像这样访问它:

    final Question question = questions[questionIndex];
    QuestionWidget(question),
    ...question.answers.map((answer) {
      return AnswerWidget(() => answerQuestion(answer.score), answer.text);
    }).toList(),
    

    【讨论】:

      猜你喜欢
      • 2019-09-23
      • 2021-11-06
      • 2021-10-02
      • 2021-08-17
      • 2021-12-10
      • 2021-10-06
      • 2020-04-15
      • 2021-10-24
      • 1970-01-01
      相关资源
      最近更新 更多