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 to validate "ToggleButton" in flutter?

As It seems there is no built-in validatior() for ToggleButton I tried to do something like following inside an ElevatedButton():

          onPressed: () {
            print('this is _selectedOptions: $_selectedOptions');
            if (_selectedOptions == [false, false, false]) {
              const SnackBar(
                duration: Duration(seconds: 5),
                content:
                    Text('You should select at least 1 option '),
              );
            } 
          }

And I see this is _selectedOptions: [false, false, false] prints out, but it doesn’t go into the if statement I don’t know why?

Please let me know why it doesn’t go into the if statement, also if there is a better way of validating ToggleButton ?

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 could change if statement.

From

if (_selectedOptions == [false, false, false])

To

if (!_selectedOptions.contains(true))

It’s because of Dart’s characteristic, check below.

https://api.flutter.dev/flutter/dart-core/List/operator_equals.html

"the equality comparison does not compare the elements of the two lists."

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