Home > Mobile >  LateInitializationError: Field 'ques' has not been initialized
LateInitializationError: Field 'ques' has not been initialized

Time:03-20

It shows this error although I have added late and required in the Question class constructor. It's repeatedly shows ======== Exception caught by widgets library ======================================================= The following LateError was thrown building _BodyBuilder: LateInitializationError: Field 'ques' has not been initialized.

Main Class##

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'QuestionsAnswers.dart';


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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: SafeArea(
        child: Scaffold(
          backgroundColor: Colors.black,
          appBar: AppBar(
            backgroundColor: Colors.grey[900],
            leading: Icon(Icons.games),
            title: Text(
              'Quizzler',
              style: TextStyle(
                fontSize: 20.0,
                fontWeight: FontWeight.bold,
                fontStyle: FontStyle.italic,
                color: Colors.white,
              ),
            ),
          ),
          body: QuizPlay(),
        ),
      ),
    );
  }
}

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

  @override
  State<QuizPlay> createState() => _QuizplayState();
}

class _QuizplayState extends State<QuizPlay> {
  List<Icon> score=[];// array of score icon
  List<Questions>questionsAndAnswers=[
    Questions(a:'Pakistan is an under developed country',b:true),
    Questions(a:'Imran Khan is the Prime Minister of Pakistan',b:true),
    Questions(a:'Y comes after U',b:false)
  ];
  int questiontracker=0;// varaible to increament of questions
  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.stretch,
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Expanded(
          flex: 4,
          child: Padding(
            padding: EdgeInsets.all(10.0),
            child: Center(
              child: Text(
                questionsAndAnswers[questiontracker].ques,
                style: TextStyle(
                  fontSize: 25.0,
                  color: Colors.white70,
                ),
              ),
            ),
          ),
        ),
        Expanded(
          child: Padding(
            padding: EdgeInsets.all(10.0),
            child: ElevatedButton(
              style: ButtonStyle(
                backgroundColor: MaterialStateProperty.all(Colors.green),
              ),
              onPressed: () {
                //Yes button
                bool answer=questionsAndAnswers[questiontracker].ans;
                if (answer==true)
                  {
                    print('correct answer');
                  }
                else
                  {
                    print('wrong answer  ');
                  }
                setState(() {
                  questiontracker  ;
                  score.add(Icon(Icons.check,color: Colors.green,)) ;
                });
              },
              child: Text(
                'Yes',
                style: TextStyle(
                  fontSize: 20.0,
                ),
              ),
            ),
          ),
        ),
        Expanded(
          child: Padding(
            padding: EdgeInsets.all(10.0),
            child: ElevatedButton(
              style: ButtonStyle(
                backgroundColor: MaterialStateProperty.all(Colors.red),
              ),
              onPressed: () {
                // No button
                bool answer=questionsAndAnswers[questiontracker].ans;
                if (answer==false)
                {
                  print('correct answer');
                }
                else
                {
                  print('wrong answer  ');
                }
                setState(() {
                  questiontracker  ;
                  score.add(Icon(Icons.close,color: Colors.red,)) ;
                });
              },
              child: Text(
                'No',
                style: TextStyle(
                  fontSize: 20.0,
                ),
              ),
            ),
          ),
        ),
        Row(
          children: score,
        ),
      ],
    );
  }
}

###Question CLASS###

class Questions{
  late String ques;
  late bool ans;
 Questions({required String a,required bool b})
 {
   a=ques;
   b=ans;
 }

}

CodePudding user response:

make it

ques = a;
ans = b;

This stores the value on the right in the value on the left.

CodePudding user response:

Your class constructor Questions is wrong, change it to:

class Questions{
  late String ques;
  late bool ans;
 Questions({required String a,required bool b}) {
   ques = a;
   and = b;
 }

}

CodePudding user response:

What is the purpose of having your questions as a plain class? I'd suggest turning it into a module class which in turn should be

class Question
{
String? ques;
bool? ans;
Question({
this.ques, this.ans});
}

and when you want to initialize a question I'd suggest creating a list

List<Question> questions = [];
question.add(Question("question",true));
// add more as you wish

This will allow you to turn it into JSON which will enable you to maybe provide questions from an online database to the app without needing to update the app every time you want to add a question.

  • Related