简体   繁体   English

问题 state 在 Firestore 中使用开关和 flutter 更改真假

[英]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.我有一个开关,应该在 isOnline 与否之间更改用户的 state。 this is a bool between true and false.这是真假之间的布尔值。 I created a method with cupertino switch:我用 Cupertino 开关创建了一个方法:

bool isOnline = true;

the original state is true so the user see they are available online原来的 state 是真的,所以用户看到它们可以在线使用

in the cupertino switch I create a function:在 Cupertino 开关中,我创建了一个 function:

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

and I need to update firestore collection:我需要更新firestore集合:

 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?一切正常,但由内而外...当我将开关转为绿色时,它会在数据库上返回false ,如果我将其关闭,则返回true有什么建议吗?

Issue in your on state code call在您的 state 代码调用中出现问题

isOnline = value;

After

changeState(); 

method.方法。

Like this,像这样,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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