简体   繁体   中英

Problem with state changing in Firestore with switch and flutter true and false

I have a switch which should change state of the user between isOnline or not. this is a bool between true and false. I created a method with cupertino switch:

bool isOnline = true;

the original state is true so the user see they are available online

in the cupertino switch I create a function:

CupertinoSwitch(
                      value: isOnline,
                      onChanged: (bool value) {
                        setState(() {
                          changeState();
                          isOnline = value;
                        });
                      },
                    ),

and I need to update firestore collection:

 CollectionReference changestate = FirebaseFirestore.instance.collection('Consultant');
  Future<void> changeState() {
    var firebaseUser =  FirebaseAuth.instance.currentUser;
    return changestate
        .doc(firebaseUser.uid)
        .update({
      'isOnline': isOnline,
    },)

all works but inside out... when I turn the switch to green it return on the database false and if I turn off it return true any suggestion please?

Issue in your on state code call

isOnline = value;

After

changeState(); 

method.

Like this,

CupertinoSwitch(
    value: isOnline,
      onChanged: (bool value) {
         setState(() { 
           isOnline = value; 
           changeState();      
          }); 
        }, 
      ), 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM