Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
389 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 Answer

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();
      }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...