Failed assertion: boolean expression must not be null flutter

  android, flutter, ios, mobile-application

im trying to get switch state but it keep showing me this error
Failed assertion: boolean expression must not be null

and this how i get it by saving switch state to shared preferences and getting the value back

  bool availability;
  
  
  Future<bool> getSwitchState() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    setState(() {
      availability = prefs.getBool('isAvailable') ??false;
    });
  }
  
  
 // and this how i save the switch state when it is changed 
  
  
   Future<bool> savingSwitchValue(bool value) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    return prefs.setBool('isAvailable', value);
  }
  
     CustomSwitch(
                  value: availability,
                  onChanged: (value) {
                    if (availability == false) {
                      setState(() {
                        availability = true;
                        savingSwitchValue(value);
                        print('this is true');
                        goOnline();
                        getLocationUpdates();
                      });
                    } else {
                      setState(() {
                        availability = false;
                        savingSwitchValue(value);
                        print('this is false');
                        goOffline();
                      });
                    }
                  },
                  activeColor: Colors.green,
                ),

how can i solve this issue

Source: Android Questions

One Reply to “Failed assertion: boolean expression must not be null flutter”

LEAVE A COMMENT