skip to Main Content

I am trying to store user credentials ( name and email ) to cloud Firestore during the time user first signs up for the app.

I am able to create new user but having trouble registering the credentials to Firestore.

Can someone pls tell me what am I doing wrong??

here’s the code sample where I am registering user.

bool isloggedin = false;
final TextEditingController _email = TextEditingController();
final TextEditingController _pswd = TextEditingController();
final TextEditingController _name = TextEditingController();

handleSubmit() {
    if (formkey.currentState!.validate()) {
        setState(() {
            isloggedin = !isloggedin;
        });
        Auth()
            .registerWithEmailAndPassword(
                name: _name.text, email: _email.text, password: _pswd.text)
            .then((value) async {
                await FirebaseFirestore.instance
                    .collection("users")
                    .doc(value ? .uid)
                    .set({
                        'uid': value ? .uid,
                        'email': _email,
                        'name': _name,
                        'password': _pswd
                    });
            });


        if (FirebaseAuth.instance.currentUser != null) {
            Navigator.of(context).pushReplacement(
                MaterialPageRoute(builder: (BuildContext context) => MyHomePage()));
        } else {
            setState(() {
                isloggedin = !isloggedin;
            });
            showDialog(
                context: context,
                builder: (context) {
                    return AlertDialog(
                        actions: < Widget > [
                            TextButton(
                                onPressed: () {
                                    Navigator.of(context).pop();
                                },
                                child: const Text(
                                    'Okay',
                                    style: TextStyle(color: Colors.blue),
                                ))
                        ],
                        title: const Text('Already Registered'),
                            content: const Text(
                                'This Email is already registered with us. Try signing in'),
                    );
                });
        }
    }
}


The handleSubmit() function is called when a user clicks on create account button .

The user gets registered , but the app does not move forward from this point. But when I press back on my phone, I get to the homepage. So, I know that I’ve been registered, plus I can see my email on Firebase auth console, but nothing new in the cloud Firestore.

2

Answers


  1. Chosen as BEST ANSWER

    Actually I found the solution. I've to add the firestore code in firebase.dart file instead of where it was before (signup.dart file), just after the part where i've called the createUserWithEmailAndPassword method of firebaseAuth.


  2. So, the issue is condition is not awaiting for you call to complete. below code will fix your issue

     handleSubmit() {            
        if (formkey.currentState!.validate()) {
          setState(() {
            isloggedin = !isloggedin;
          });
          Auth()
              .registerWithEmailAndPassword(
                  name: _name.text, email: _email.text, password: _pswd.text)
              .then((value) async {
            await FirebaseFirestore.instance
                .collection("users")
                .doc(value?.uid)
                .set({
              'uid': value?.uid,
              'email': _email,
              'name': _name,
              'password': _pswd
            });
           //You need to check condition here like below
           if (FirebaseAuth.instance.currentUser != null) {
            Navigator.of(context).pushReplacement(
                MaterialPageRoute(builder: (BuildContext context) => MyHomePage()));
          } else {
            setState(() {
              isloggedin = !isloggedin;
            });
            showDialog(
                context: context,
                builder: (context) {
                  return AlertDialog(
                    actions: <Widget>[
                      TextButton(
                          onPressed: () {
                            Navigator.of(context).pop();
                          },
                          child: const Text(
                            'Okay',
                            style: TextStyle(color: Colors.blue),
                          ))
                    ],
                    title: const Text('Already Registered'),
                    content: const Text(
                        'This Email is already registered with us. Try signing in'),
                  );
                });
          }
          
          });
        }
        }
      }
    

    So, in the code I’ve moved if (FirebaseAuth.instance.currentUser != null) exactly after you’re adding the data in firestorm and inside then.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search