Reputation: 311
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?
Upvotes: 0
Views: 397
Reputation: 5973
Issue in your on state code call
isOnline = value;
After
changeState();
method.
Like this,
CupertinoSwitch(
value: isOnline,
onChanged: (bool value) {
setState(() {
isOnline = value;
changeState();
});
},
),
Upvotes: 2