Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I add snackbar?

I dont know why the Snackbar doesnt show up.But when I delete the function off the firebaseauth it shows the snackbar.

Code:

MaterialButton(
              onPressed: (){
                try{
                  FirebaseAuth.instance.createUserWithEmailAndPassword(
                      email: emailController.text, password: passwordController.text).then((signedUser) {
                    userCollection.doc(signedUser.user?.uid).set({
                      'username': usernameController.text,
                      'email': emailController.text,
                      'password': passwordController.text,
                      'uid': signedUser.user?.uid,
                    });
                    Navigator.of(context).pushReplacement(MaterialPageRoute(builder: (_) => HomeScreen()));
                  });
                } catch(e) {
                  print(e.toString());
                  var snackbar = SnackBar(content: Text(e.toString()));
                  ScaffoldMessenger.of(context).showSnackBar(snackbar);
                }
              },
              elevation: 0,
              minWidth: MediaQuery.of(context).size.width,
              height: 40.h,
              child: Text("Buat akun"),
              textColor: Colors.white,
              color: Colors.blue,
            )

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

you are using then so the error won’t be caught in the catch section.

You should remove the try catch block and catch the error with catchError() instead like this:

FirebaseAuth.instance.createUserWithEmailAndPassword(
                      email: emailController.text, password: passwordController.text).then((signedUser) {
                    userCollection.doc(signedUser.user?.uid).set({
                      'username': usernameController.text,
                      'email': emailController.text,
                      'password': passwordController.text,
                      'uid': signedUser.user?.uid,
                    });
                    Navigator.of(context).pushReplacement(MaterialPageRoute(builder: (_) => HomeScreen()));
                  }).catchError((e) {
                    print(e.toString());
                    var snackbar = SnackBar(content: Text(e.toString()));
                    ScaffoldMessenger.of(context).showSnackBar(snackbar);
                  });
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading