Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can i check on null?

I have void func:

//change state in select
    void _onChangeSelect(Color? newValue) {
      setState(() {
        _selectedValue = newValue!;
      });
    }

How can I check for null without "!", but with "??".

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

you need to provide a fallback then, a color to use when it is null. for example

//change state in select
    void _onChangeSelect(Color? newValue) {
      setState(() {
        _selectedValue = newValue ?? Colors.white;
      });
    }

or if you want to keep the old value

//change state in select
    void _onChangeSelect(Color? newValue) {
      setState(() {
        _selectedValue = newValue ?? _selectedValue ;
      });
    }

or simply write this

//change state in select
    void _onChangeSelect(Color? newValue) {
      setState(() {
        if (newValue != null) {
          _selectedValue = newValue;
        }
      });
    }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading