skip to Main Content

I am trying to authenticate a user using email, password and username as additional info, but it’s giving me an error saying:

too many positional argument

final userCredential  = await FirebaseAuth.instance
    .createUserWithEmailAndPassword(
  email: email,
  password: password,
  AdditionalUserInfo(isNewUser: true, username: firstName)
);

I tried like this but it doesn’t work

final userCredential = await FirebaseAuth.instance.createUserWithEmailAndPassword(
    email: email,
    password: password,
    additionalUserInfo: AdditionalUserInfo(isNewUser: true, username: firstName),
);

2

Answers


  1. Remove additionalUsreInfo: AdditionalUserInfo(...). The .createUserWithEmailAndPassword only expects email and password as parameters.

    final userCredential  = await FirebaseAuth.instance .createUserWithEmailAndPassword( email: email, password: password);
    

    Update

    To add user data after logging in, you need to first check if the user is actually been created/ logged in (check auth state changes) and then update the user data accordingly. There are various ways of going about it. For your use case, we can have something like this:

    final userCredential = await FirebaseAuth.instance
        .createUserWithEmailAndPassword(email: email, password: password)
        .then((user) async {
      await user.user?.updateDisplayName(firstName);
    });
    

    So basically, we are waiting for the user creation to be done, take advantage of its Future property and set the desired name. Since we are creating the user for the first time, we don’t need to specify that user is a new user; Firebase checks that for us.

    There are other functions that can be utilised to set other variables too.

    AdditionalUserInfo() is used in what is called the federated identity provider to provide extra details about accounts that are created through other platform accounts. It allows you to add more data to bind the identity of the user.

    Login or Signup to reply.
  2. While @TrueKing will work, please note that there is another operation available that can be performed without user interaction. You can save some additional data in the Firebase user object or if you want directly in Firestore using Cloud Function for Firebase. This can be done using onCreate() as explained in the official documentation related to triggering a function on user creation.

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