Home > Blockchain >  FS Document is not getting created after user is registered
FS Document is not getting created after user is registered

Time:01-12

When a user submits their information on the "Signup page", the user should have

  1. an account registered within fire store and
  2. a fire store document created with all of their information inside.

The user account gets registered fine, but the document never gets created. I'm not getting any error messages so I'm trying to use debug prints to find out where things are going wrong.

Debug Prints:

>> login: signUp // this starts the signUp function
>> login: Start hideNewUserOverlay // hide overlay prints before signUp finishes
>> signUp: current user got // the following prints are from signUp
>> signUp: user.id = L8pD6tng5NTAACN7VygK93F6crg1 
>> signUp: document creation code Start // then nothing happens after this

Future that is supposed to register the user and create document: // this will eventually pass in first/last names too, that's why I'm using this function

Future<void> signUp(String email, String password) async {
  try {
    // ignore: unused_local_variable
    UserCredential result = await auth.createUserWithEmailAndPassword(email: email, password: password); // <-- user account is created on first press
  } catch (e) {
    debugPrint('>> Authentication: create new user error');
  }
  user = auth.currentUser!;
  debugPrint('>> signUp: current user got');
  String userID = user.uid;
  debugPrint('>> signUp: user.id = $userID');  // all debugs print out correctly here, even userID
  
  debugPrint('>> signUp: document creation code Start');
  await collectionReference.doc(userID).set({ // code does not run
    'userID': userID,
    'accountCreated': DateTime.now(),
    'email': email,
  });
  debugPrint('>> Authentication: User Document Created');
}

Signup page:

onPressed: () {
    debugPrint('>> login: signUp');
    signUp(_email, _password); // this line should finish before the next debug statement is printed but it does not
    debugPrint('>> login: Start hideNewUserOverlay'); // prints before signUp() is done
    hideNewUserOverlay(); // this will close the Signup page
    Navigator.of(context).pushReplacement(MaterialPageRoute(
        builder: (context) => const Nav(),  
    ));
}

At the bottom of the code, the document will get created if I route to a different class. Nav() >> Verify(). The weird part is that Verify does not take in any user information. Verify() has a timer so maybe that has something to do with it? I think it is because the signup() function does not complete before the overlay is hidden. Maybe Nav() needs init state?

CodePudding user response:

Putting await in from of a statement that returns a Future makes that line block the rest of the execution. It does not however make any other call wait.

If you want to wait until signUp is done, use await there too:

await signUp(_email, _password);

That does mean you'll need to mark onPressed as an async method too.

If that is not an option, you can always use then:

onPressed: () {
    debugPrint('>> login: signUp');
    signUp(_email, _password).then(() {
        debugPrint('>> login: Start hideNewUserOverlay'); // prints before signUp() is done
        hideNewUserOverlay(); // this will close the Signup page
        Navigator.of(context).pushReplacement(MaterialPageRoute(
            builder: (context) => const Nav(),  
        ));
   }
}
  •  Tags:  
  • Related