0

enter image description here

This is the flutter code that I wrote 2 years ago. But I couldn't make any sense of why it has stopped working now.

How do I fix this exception.

import './question.dart';
import './answer.dart';

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

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

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Question(
          questions[questionIndex]['questionText'] as String,
        ),
        ...(questions[questionIndex]['answers'] as List<Map<String, dynamic>?>)
            .map((answer) {
          return Answer(
              () => answerQuestion(answer['score']), answer!['text'] as String);
        }).toList()
      ],
    );
  }
}
3
  • This is the GitHub: github.com/Ankitkj1999/gfg_quiz/blob/master/lib/quiz.dart Commented Dec 3, 2022 at 13:57
  • could you include your print("data = ${questions[questionIndex]}") result? Commented Dec 3, 2022 at 14:10
  • You do know that Dart has null safety now? Two years ago it didn't. Commented Dec 3, 2022 at 14:18

2 Answers 2

1

Change this:

...(questions[questionIndex]['answers'] as List<Map<String, dynamic>?>)

to this:

...(questions[questionIndex]['answers'] as List<Map<String, dynamic>?>?)

this happened because questions[questionIndex]['answers'] is null and can't cast null to List<Map<String, dynamic>?>.

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

3 Comments

I had to do this to solve other exceptions. Now it doesn't show any error but the widget is not getting rendered in the screen. ...?(questions[questionIndex]['answers'] as List<Map<String, dynamic>?>?) ?.map((answer) { return Answer( () => answerQuestion(answer['score']), answer!['text'] as String); }).toList()
could you include your print("data = ${questions[questionIndex]}") result? I think the data you looking for is empty and that is why you widget doesn't show up.
yes it is null, but we have initialized question in the code.
1

I cloned your repo from GitHub, replaced quiz.dart with your code from the topic. There are things that must be done since some widgets are outdated:

  1. Change RaisedButton to ElevatedButton (with the removal of color and textColor properties)

  2. Change FlatButton to TextButton (with the removal of textColor property)

Other changes to pubspec.yaml:

environment:
  sdk: ">=2.19.4 <3.0.0"

instead of sdk: ">=2.7.0 <3.0.0"

in result.dart change Function to VoidCallback:

  final VoidCallback resetHandler;

in answers.dart change Function to VoidCallback:

  final VoidCallback selectHandler;

And now, the code you provided here executed normally.

Comments

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.