Ene Cristian Luis
Ene Cristian Luis

Reputation: 55

Null uid on firestore attempt to save data with flutter and firebase

I am trying to save some data when a user registers but I may not be calling the FirebaseUser right. Any suggestions?

Code:

String errorMessage;
  String name;
  String password;
  String email;
  String role;
  bool obscure = true;
  FirebaseUser instructor;

  final FirebaseAuth _auth = FirebaseAuth.instance;

  final db = Firestore.instance;

  void saveData() async {
    await db.collection("instructori")
      .document(instructor.uid)
      .setData(
        {
          'Nume': name,
          'Rol': role,
          'Email': email,
          'Parola': password,
          'UID': instructor.uid
        }
      );
  }


  Future<void> registerUser() async {
    instructor = await _auth
        .createUserWithEmailAndPassword(
      email: email,
      password: password,
    ).then((onValue) {
      saveData();
      Navigator.of(context).pushNamed(ClientiPage.id);
    }).catchError((error) {
      errorMessage = error;
    });



    Navigator.of(context).pushNamed(ClientiPage.id);
  }

The user gets authenticated sucessfully but I don't see data being saved to the firestore tree. I got everything working in the console so I assume it is the code.

Error:

E/flutter ( 5527): Tried calling: uid
E/flutter ( 5527): #0      Object.noSuchMethod  (dart:core-patch/object_patch.dart:53:5)
E/flutter ( 5527): #1      _RegisterPageState.saveData 
package:euroswimming_instructori/register_page.dart:29
E/flutter ( 5527): #2      _RegisterPageState.registerUser.<anonymous closure> 
package:euroswimming_instructori/register_page.dart:48
E/flutter ( 5527): #3      _rootRunUnary  (dart:async/zone.dart:1134:38)

Upvotes: 1

Views: 755

Answers (2)

Peter Haddad
Peter Haddad

Reputation: 80904

You should do:

instructor = (await FirebaseAuth.instance.currentUser()).uid;

to be able to get the uid of the user.

currentUser returns a value of type Future<FirebaseUser>, therefore you use await to be able to retrieve the currentUser.

https://dart.dev/codelabs/async-await#working-with-futures-async-and-await

https://github.com/FirebaseExtended/flutterfire/blob/master/packages/firebase_auth/firebase_auth/lib/src/firebase_auth.dart#L314

Note:

uid is of type String therefore if you want instructor to be of type FirebaseUser then do the following:

instructor = await FirebaseAuth.instance.currentUser();
String userId = instructor.uid;

Upvotes: 2

stwupton
stwupton

Reputation: 2235

The .then() was returning a different Future<void>, so instructor was being set to null. You can just set the instructor in the .then() callback like so:

void registerUser() {
  _auth.createUserWithEmailAndPassword(
    email: email,
    password: password,
  ).then((result) {
    instructor = result.user;
    saveData();
    Navigator.of(context).pushNamed(ClientiPage.id);
  }).catchError((error) {
    errorMessage = error;
  });

  Navigator.of(context).pushNamed(ClientiPage.id);
}

Upvotes: 2

Related Questions