Home > Software design >  How do I add snackbar?
How do I add snackbar?

Time:04-07

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,
            )

CodePudding user response:

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);
                  });
  • Related