Errors in dart class

222

First of all you can omit this part.

domande = d;
risposte = r;

The pattern of assigning a constructor argument to an instance variable is so common, but with dart you can just do it like the code snippet below.
Now we come to your problem. First of all you are using null-safety and created you variable to not be nullable. Then you created your constructors with optional parameters, this means those parameters can be null, but you class variables can't be null. This is the reason why you this error occurs.
You can fix this by using the keyword required which means you variables are mandatory.

class Domande {
  String domande;
  bool risposte;

  Domande({required this.domande, required this.risposte});
}

Another way to fix this is to make your variables nullable by using the ?. But then make sure you handle the case if those variables are null.

class Domande {
  String? domande;
  bool? risposte;

  Domande({this.domande, this.risposte});
}
Share:
222
Info TechCourse
Author by

Info TechCourse

Updated on December 31, 2022

Comments

  • Info TechCourse
    Info TechCourse over 1 year

    So I'm working on this quiz app, and I just create a class for my question and answers. But vscode keeps telling me that there are errors. Can someone please help me?

    Here is the main.dart

    import 'package:flutter/material.dart';
    import 'question.dart';
    
    void main() {
      runApp(HomePage());
    }
    
    class HomePage extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          debugShowCheckedModeBanner: false,
          home: Scaffold(
            backgroundColor: Colors.grey[900],
            body: SafeArea(
              child: Padding(
                padding: EdgeInsets.symmetric(horizontal: 10.0),
                child: Quizzler(),
              ),
            ),
          ),
        );
      }
    }
    
    class Quizzler extends StatefulWidget {
      @override
      QuizzlerState createState() => QuizzlerState();
    }
    
    class QuizzlerState extends State<Quizzler> {
      List<Widget> scoreKeeper = [];
    
      List<Domande> domandeBank = [
        Domande(d: 'Il sole è una stella', r: true),
        Domande(d: 'Il latte è verde', r: false),
        Domande(d: 'Il mare è blu', r: true),
      ];
    
      int qNumber = 0;
    
      @override
      Widget build(BuildContext context) {
        return Column(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: <Widget>[
            Expanded(
              flex: 5,
              child: Padding(
                padding: const EdgeInsets.all(10.0),
                child: Center(
                  child: Text(
                    domandeBank[qNumber].domande,
                    style: TextStyle(color: Colors.white),
                    textAlign: TextAlign.center,
                  ),
                ),
              ),
            ),
            Expanded(
              child: Padding(
                padding: const EdgeInsets.all(10.0),
                child: TextButton(
                  style: TextButton.styleFrom(padding: EdgeInsets.zero),
                  child: Container(
                    color: Colors.green,
                    height: 50,
                    width: double.infinity,
                    child: Center(
                      child: Text(
                        "True",
                        style: TextStyle(color: Colors.white),
                      ),
                    ),
                  ),
                  onPressed: () {
                    setState(() {
                      bool risCorretta = domandeBank[qNumber].risposte;
                      if (risCorretta == true) {
                        scoreKeeper.add(
                          Icon(
                            Icons.check,
                            color: Colors.green,
                          ),
                        );
                      } else {
                        scoreKeeper.add(
                          Icon(
                            Icons.close,
                            color: Colors.red,
                          ),
                        );
                      }
                      qNumber++;
                    });
                  },
                ),
              ),
            ),
            Expanded(
              child: Padding(
                padding: const EdgeInsets.all(10.0),
                child: TextButton(
                  style: TextButton.styleFrom(padding: EdgeInsets.zero),
                  child: Container(
                    color: Colors.red,
                    height: 50,
                    width: double.infinity,
                    child: Center(
                      child: Text(
                        "False",
                        style: TextStyle(color: Colors.white),
                      ),
                    ),
                  ),
                  onPressed: () {
                    setState(() {
                      bool risCorretta = domandeBank[qNumber].risposte;
                      if (risCorretta == false) {
                        scoreKeeper.add(
                          Icon(
                            Icons.check,
                            color: Colors.green,
                          ),
                        );
                      } else {
                        scoreKeeper.add(
                          Icon(
                            Icons.close,
                            color: Colors.red,
                          ),
                        );
                      }
                      qNumber++;
                    });
                  },
                ),
              ),
            ),
            Row(
              children: scoreKeeper,
            ),
          ],
        );
      }
    }
    

    And here is the question.dart class

    class Domande {
      String domande;
      bool risposte;
    
      Domande({String d, bool r}) {
        domande = d;
        risposte = r;
      }
    }
    

    And the errors i get:

    Non-nullable instance field 'domande' must be initialized.
    Try adding an initializer expression, or add a field initializer in this constructor, or mark it 'late'.
    
    
    Non-nullable instance field 'risposte' must be initialized.
    Try adding an initializer expression, or add a field initializer in this constructor, or mark it 'late'.
    
    
    The parameter 'd' can't have a value of 'null' because of its type, but the implicit default value is 'null'.
    Try adding either an explicit non-'null' default value or the 'required' modifier.
    
    The parameter 'r' can't have a value of 'null' because of its type, but the implicit default value is 'null'.
    Try adding either an explicit non-'null' default value or the 'required' modifier.