type 'List<Object>' is not a subtype of type 'List<Map<String, Object>>' in type cast

Here's my code on quiz.dart

class Quiz extends StatelessWidget {
  final List<Map<String, Object>> questions;
  final int question_index;
  final void Function() answerquestions;
  Quiz(
      {required this.questions,
      required this.answerquestions,
      required this.question_index});

  @override
  Widget build(BuildContext context) {
    return (Column(
      children: [
        Question(questions[question_index]['QuestionText'] as String),
        ...(questions[question_index]['Answers'] as List<Map<String, Object>>)
            .map((answer) {
          return Answers(answerquestions, answer['text'] as String);
        }).toList()
      ],
    ));
  }
}

and this is my code on answer.dart

class Answers extends StatelessWidget {
  final void Function() selectHandler;
  final String answerText;

  Answers(this.selectHandler, this.answerText);

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      child: RaisedButton(
        color: Colors.blue,
        textColor: Colors.white,
        onPressed: selectHandler,
        child: Text(answerText),
      ),
    );
  }
}

i got and error that says type 'List' is not a subtype of type 'List<Map<String, Object>>' in type cast

the error says I got wrong on this line

...(questions[question_index]['Answers'] as List<Map<String, Object>>)
            .map((answer) {
          return Answers(answerquestions, answer['text'] as String);
        }).toList()

Upvotes: 0

Views: 40

Answers (1)

Munsif Ali
Munsif Ali

Reputation: 5842

actually questions is a type List<Map<String,Object>> and you are accessing questions[index]["Answers"] which is of type Object so it can not be used as List<Map<String,Objecct>>

Upvotes: 1

Related Questions