Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
390 views
in Technique[技术] by (71.8m points)

Can't dimiss showdialog with setstate flutter

I want to verify if the phone number the user enter is in firebase, so I am using a showDialog to show a spinner, I want to dismiss the showdialog if that phone number already exist. I have tried setState to but the showddialog won't dismiss automatically avail

_phoneExist(phone) {
    if (loading) {
      showDialog(
          context: context,
          builder: (c) {
            return Center(
              child: SpinKitChasingDots(
                color: Colors.brown,
                size: 50.0,
              ),
            );
          });
    }
    firestoreInstance
        .collection("partners")
        .where("phone", isEqualTo: phone)
        .getDocuments()
        .then((value) {
      value.documents.forEach((result) {
        if (result.exists) {
          print(result['phone']);
          setState(() {
            _userExist = true;
            loading = false;
          });
        } else {
          print('ghhgghgh');
        }
      });
    });

    if (_userExist) {
      Scaffold.of(context).showSnackBar(snackBar2('$phone Aleady Exists'));
      setState(() {
        _userExist = false;
      });
    } else {
      print('gghhgghgchgc');
      print(phone);
      // _registerUser();
    }
  }
question from:https://stackoverflow.com/questions/65626004/cant-dimiss-showdialog-with-setstate-flutter

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can try with the below lines

      BuildContext buildContext;

     _phoneExist(phone) {
      if (loading) {
        showDialog(
            context: context,
            builder: (BuildContext context) {
              buildContext = context;   // Assign your context to buildcontext
              return Center(
                child: SpinKitChasingDots(color: Colors.brown,size: 50.0,
                ),
              );
            });
      }
      firestoreInstance
          .collection("partners").where("phone", isEqualTo: phone).getDocuments()
          .then((value) {
        value.documents.forEach((result) {
          if (result.exists) {
            setState(() {
              _userExist = true;
              loading = false;

              
                  SchedulerBinding.instance.addPostFrameCallback((_) {
                       Navigator.pop(buildContext);
                  });  // By adding this you can close your pop up


            });
          } else {
          }
        });
      });
  
      if (_userExist) {
        Scaffold.of(context).showSnackBar(snackBar2('$phone Aleady Exists'));
        setState(() {
          _userExist = false;
        });
      } else {
        // _registerUser();
      }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...