Search code examples
flutterdartflutter-alertdialog

How to make Alert Dialog working in flutter


I am using below code to register the user, there is an option to upload image , if it is null the user won't be registered, that is working accurately, only thing I am facing is that if the image is null it should the alert dialog to the user , but the alert dialog is not working at all. How should I implement the alert dialog?

if (avatarImageFile != null ) {
                  FirebaseAuth.instance
                      .createUserWithEmailAndPassword(
                      email: emailInputController.text,
                      password: pwdInputController.text)
                      .then((currentUser) =>

                      Firestore.instance
                          .collection("users")
                          .document(currentUser.user.uid)
                          .setData({
                        "username": userNameInputController.text,
                        "email": emailInputController.text,
                      })
                          .then((result) =>
                      {

                      uploadFile(currentUser.user.uid),

                      print(currentUser.user.getIdToken()),
                        currentUser.user.sendEmailVerification(),
                        Navigator.pushAndRemoveUntil(
                            context,
                            MaterialPageRoute(
                                builder: (context) =>
                                    MainScreen(
                                    )),
                                (_) => false),
                      })
                          .catchError((err) => print(err)))
                      .catchError((err) => print(err));

                }
                else {
                  print("Please Upload the Image");
                  AlertDialog(
                          title: Text("Image Required"),
                          content: Text("Please upload the image"),
                          actions: <Widget>[
                            FlatButton(
                              child: Text("Close"),
                              onPressed: () {
                                Navigator.of(context).pop();
                              },
                            )
                          ],
                        );
                }

Solution

  • You need to use the function showDialog so the dialog appears:

    else {
       print("Please Upload the Image");
         showDialog(
         context: context,
         builder: (BuildContext context) {
             return AlertDialog(
                     title: Text("Image Required"),
                     content: Text("Please upload the image"),
                      actions: <Widget>[
                       FlatButton(
                         child: Text("Close"),
                         onPressed: () {
                          Navigator.of(context).pop();
                          },
                       )
                      ],
                    );
                   };
                 );
               }