0

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()
2
  • 1
    Can you attach your json result? Commented Jul 8, 2022 at 7:02
  • Welcome to SO! Please Edit your question by clicking on the Edit link below the question itself and add the JSON that populates the Quiz.questions property. Commented Jul 8, 2022 at 21:09

1 Answer 1

1

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>>

Sign up to request clarification or add additional context in comments.

2 Comments

what should i do to fix the error tho?
"as List<Map<String, Object>>" remove this hope this will work. if didn't then let me know

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.