Dart make sure optional boolean in class is not null

9,456

Solution 1

As the fields are final try passing the default during constructor initialization.

Also add an assert statement to make sure it's never initialized to null.

Example:

class QText extends StatelessWidget {
  QText(this.text, this.size, [this.bold = false]) : assert(bold != null);

  final String text;
  final double size;
  final bool bold;

  @override
  Widget build(BuildContext context) {}
}

Hope that helps!

Solution 2

For now, all types in Dart are nullable. Even with a default value, someone can explicitly pass null as argument. If you want to be absolutely certain that the value is not null, you must guard all ways to set the value. In the original example the bold variable was public and not final, which means that you must also guard the setter.

Example with final variable:

class QText extends StatelessWidget {
  QText(this.text, this.size, [bool bold]) : this.bold = bold ?? false;

  final String text;
  final double size;
  final bool bold;
  ...
}

Example with settable bold:

class QText extends StatelessWidget {
  QText(this.text, this.size, [bool bold]) : this.bold = bold ?? false;

  final String text;
  final double size;
  bool _bold;

  bool get bold => _bold;
  void set bold(bool bold) { this._bold = bold ?? false; }
  ...
}

You can use a default value, but it doesn't buy you anything security-wise. It is useful as documentation, though.

Solution 3

You need to define your default value for variable inside the constructor:

Like below:

QText(this.text, this.size, [this.bold = false]): assert(bold != null,"bold cannot be null.");

If nothing is passed in bold variable then it will take default value as false.

EDIT

You can use the assertion to prevent user to pass null, you can also print a message if assertion fails.

assert(bold != null,"bold cannot be null.")
Share:
9,456
Hasen
Author by

Hasen

Updated on December 12, 2022

Comments

  • Hasen
    Hasen over 1 year

    Since it's an optional value, it will sometimes be null, so how can I avoid it throwing an error? Is this the only way to do it?

    class QText extends StatelessWidget {
      QText(this.text, this.size, [this.bold]);
    
      final String text;
      final double size;
      bool bold;
    
      @override
      Widget build(BuildContext context) {
        bold = (bold == null) ? false : bold;
        return Whatever..
      }
    }
    

    I'd like to do this but of course it doesn't work:

    class QText extends StatelessWidget {
      QText(this.text, this.size, [this.bold]);
    
      final String text;
      final double size;
      final bool bold ?? false;
    
      @override
      Widget build(BuildContext context) {
        return Whatever..
      }
    }
    
  • Hasen
    Hasen almost 5 years
    Really? So you can do that just like in Javascript. That's cool. Don't know why I didn't even try that lol.
  • jamesdlin
    jamesdlin almost 5 years
    This does not prevent a caller from explicitly passing null as an argument.
  • ibhavikmakwana
    ibhavikmakwana almost 5 years
    Updated answer.