Home > Enterprise >  The argument type 'Object?' can't be assigned to the parameter type 'String'
The argument type 'Object?' can't be assigned to the parameter type 'String'

Time:05-06

It throws an error The argument type 'Object?' can't be assigned to the parameter type 'String' at answer['text']);

  class Quiz extends StatelessWidget {
  final Function answerQuestion;
  final int questionIndex;
  final List<Map<String, Object>> questions;

  Quiz(
      {required this.answerQuestion,
      required this.questionIndex,
      required this.questions});

    @override
      Widget build(BuildContext context) {
        return Column(
          children: [
            Questions(
              questions[questionIndex]['questionText'].toString(),
            ),
            ...(questions[questionIndex]['answers'] as List<Map<String, Object>>)
                .map((answer) {
              return Answer(() => answerQuestion(answer['score']), answer['text']);
            }).toList()
          ],
        );
      }
    }

CodePudding user response:

answer is Map<String, Object>>, answer['text'] is Object, answerQuestion expects that the second parameter is String.

CodePudding user response:

You want to cast your answer["text"] Object to a String. Change this:

...
              return Answer(() => answerQuestion(answer['score']), answer['text']);
            }).toList()
...

to this:

...
          return Answer(
              () => answerQuestion(answer["score"]), answer["text"] as String); // casting to String here...
        })
...
  • Related