Non-nullable instance field ‘questionAnswer’ must be initialized. 25k+

Dart can see initialization in three ways: Non-nullable instance field ‘questionAnswer’ must be initialized.
Generate a warning instead of returning null Non-nullable instance field ‘questionAnswer’ must be initialized. Use the @safe annotation to declare that a variable will not be initialized during the time that it is visible Decide that you have actually assigned a variable to count
By default, only the first type is checked. This is why you may get null s with Flutter. If you’re trying to create a javascript function that does something like this:

Run Log Is this Question:

Error Log is:

Performing hot restart...
Asset manifest contains a null or empty uri.
Error: unable to find directory entry in pubspec.yaml: F:\quizzler_flutter\images\
Waiting for connection from debug service on Chrome...
lib/question.dart:2:10: Error: Field 'questionText' should be initialized because its type 'String' doesn't allow null.
  String questionText;
         ^^^^^^^^^^^^
lib/question.dart:3:8: Error: Field 'questionAnswer' should be initialized because its type 'bool' doesn't allow null.
  bool questionAnswer;
       ^^^^^^^^^^^^^^
Non-nullable instance field 'questionAnswer' must be initialized.

Your Question Like below Pic:

Non-nullable instance field 'questionAnswer' must be initialized.

Problem is that ?

class Question {
  String questionText;
  bool questionAnswer;

  Question({required String q, required bool a}) {
    questionText = q;
    questionAnswer = a;
  }
}

Change to Question name to late valiable assign and after running fix and your error is like this Non-nullable instance field ‘questionAnswer’ must be initialized.

solved done and your error has been solved done. thank you.

Answer 2:

With null safety, Dart has no way of knowing if you had actually assigned a variable to count. Dart can see initialization in three ways Non-nullable instance field ‘questionAnswer’ must be initialized.:

  1. At the now of declaration:
  2. int count = 0;
  3. In the initializing formals text:
  4. question(this.count);
  5. In the initializer list:
  6. question() : count = 0;

Answer 3:

These are new rules about Dart about null safety Non-nullable instance field ‘questionAnswer‘ must be initialized.

Answer 4:

Use the late keyword to initialize a variable when it is first read, rather than when it’s created. in Question.dart

class Question {
  late String questionText;
  late bool questionAnswer;

  Question({required String q, required bool a}) {
    questionText = q;
    questionAnswer = a;
  }
}

Read Carefully and get this solve only assign to a late variable flutter

  1. Use the late keyword: class Quetion { late int count; // No error void bar() => count = 0; }
  2. Answer Help This:
  3. Make variable nullable: class Question { int? count; // No error void bar() => count = 0; }

Main.dart

import 'package:flutter/material.dart';
import 'package:quizzler_flutter/question.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        backgroundColor: Colors.black87,
        body: SafeArea(
          child: Padding(
            padding: EdgeInsets.symmetric(horizontal: 10.0),
            child: QuizPage(),
          ),
        ),
      ),
    );
  }
}

class QuizPage extends StatefulWidget {
  const QuizPage({Key? key}) : super(key: key);

  @override
  _QuizPageState createState() => _QuizPageState();
}

class _QuizPageState extends State<QuizPage> {
  List<Icon> scoreKeeper = [];

  List<Question> questionbank = [
    Question(q: 'My name is Eva Samntha ?', a: true),
    Question(q: 'I am FullStack Developer ?', a: false),
    Question(q: 'My Blog Site is Laratuto.com', a: true),
  ];
  var questionnumber = 0;
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Center(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: <Widget>[
            Expanded(
              flex: 5,
              child: Center(
                child: Text(
                  questionbank[questionnumber].questionText,
                  textAlign: TextAlign.center,
                  style: TextStyle(fontSize: 25.0, color: Colors.white),
                ),
              ),
            ),
            GestureDetector(
              onTap: () {
                setState(() {
                  bool correctanswer =
                      questionbank[questionnumber].questionAnswer;
                  print(correctanswer);
                  if (correctanswer == true) {
                    scoreKeeper.add(
                      Icon(
                        Icons.check,
                        color: Colors.green,
                      ),
                    );
                  } else {
                    scoreKeeper.add(
                      Icon(
                        Icons.close,
                        color: Colors.red,
                      ),
                    );
                  }
                  questionnumber++;
                });
              },
              child: Container(
                height: 50,
                margin: const EdgeInsets.all(15.0),
                decoration: BoxDecoration(
                  color: Colors.green,
                ),
                child: Center(
                  child: const Text(
                    'True',
                    style: TextStyle(fontSize: 20.0, color: Colors.white),
                  ),
                ),
              ),
            ),
            GestureDetector(
              onTap: () {
                setState(() {
                  bool correctanswer =
                      questionbank[questionnumber].questionAnswer;
                  print(correctanswer);
                  if (correctanswer == false) {
                    scoreKeeper.add(
                      Icon(
                        Icons.check,
                        color: Colors.green,
                      ),
                    );
                  } else {
                    scoreKeeper.add(
                      Icon(
                        Icons.close,
                        color: Colors.red,
                      ),
                    );
                  }
                  questionnumber++;
                });
              },
              child: Container(
                height: 50,
                margin: const EdgeInsets.all(15.0),
                decoration: BoxDecoration(
                  color: Colors.red,
                ),
                child: Center(
                  child: const Text(
                    'False',
                    style: TextStyle(fontSize: 20.0, color: Colors.white),
                  ),
                ),
              ),
            ),
            Row(
              children: scoreKeeper,
            )
          ],
        ),
      ),
    );
  }
}

Hello I hope your error is resolved, and ping comment if your error solved him thank you. wish you…. best wishies for me and you hello bye

Leave a Comment