title: How to match Firebase User UID with Firestore Doc ID - Flutter
body: <p>I am new to flutter. So Please kindly bear with me.</p>
  <p>I am creating a Real Estate App using Firebase and Provider.
  I have two root Collections (1) &quot;users&quot; and (2) &quot;properties&quot;</p>
  <p>I would like to achieve one specific task.
  Although it seems like a simple task, yet I am still unable to solve it.
  The task is to store Firebase User UID as Firestore Document ID of the rool Collection called &quot;users&quot; when the user sign up.</p>
  <p>The problem I am having is the Firestore Document ID is automatically generated and its not Firebase User UID. Plus the field for userId appears null in Firestore.
  Please see the screenshot of my database <a href="https://paste.pics/4c48fc2c043b81522a69fe005a87b217" rel="nofollow noreferrer">here</a></p>
  <p>Thank you for your attention and time.</p>
  <p>Here is my user_model.dart</p>
  <pre><code>class NayyaaUser {
    String? uid;
    String name;
    String email;
    String? phone;
  
    NayyaaUser({this.uid, required this.name, required this.email, this.phone});
  
    //send data to Firestore
    Map&lt;String, dynamic&gt; toMap() {
      return {
        'userId': uid,
        'userName': name,
        'userEmail': email,
        'userPhone': phone,
      };
    }
  
    //draw data from firestore
    factory NayyaaUser.fromFirestore(Map&lt;String, dynamic&gt; firestore) =&gt;
        NayyaaUser(
          uid: firestore['userId'],
          email: firestore['userEmail'] ?? &quot; &quot;,
          name: firestore['userName'] ?? &quot; &quot;,
          phone: firestore['userPhone'] ?? &quot; &quot;,
        );
  } 
  </code></pre>
  <p>Here is my user_provider.dart</p>
  <pre><code>class UserProvider extends ChangeNotifier {
    final firestoreService = FirestoreService();
    final authService = AuthService();
  
  
    String? _userId;
    String? _name;
    String? _email;
    String? _phone;
  
  
    //getters
    String? get userId =&gt; _userId;
    String? get name =&gt; _name;
    String? get email =&gt; _email;
    String? get phone =&gt; _phone;
  
    //setters
    changeName(String value) {
      _name = value;
      notifyListeners();
    }
  
    changeEmail(String value) {
      _email = value;
      notifyListeners();
    }
  
    changePhone(String value) {
      _phone = value;
      notifyListeners();
    }
  
    saveUserProfile() {
      if (_userId == null) {
        var updateUserProfile = NayyaaUser(
            // userId: _userId,
            uid: _userId,
            name: name ?? '',
            email: email ?? '',
            phone: phone ?? '');
        firestoreService.saveUserDataToFirestore(updateUserProfile);
      } else {
        var newUserProfile = NayyaaUser(
            // userId: _userId,
            uid: _userId,
            name: name ?? '',
            email: email ?? '',
            phone: phone ?? '');
        firestoreService.saveUserDataToFirestore(newUserProfile);
      }
    }
  }
  </code></pre>
  <p>Here is auth_service.dart</p>
  <pre><code>class AuthService {
    final FirebaseAuth _authInstance = FirebaseAuth.instance;
  //create user obj based on &quot;User&quot; from Firebase
    NayyaaUser? _userFromFirebase(User? user) {
      return user != null
          ? NayyaaUser(
              uid: user.uid,
              name: '',
              email: '',
              phone: '',
            )
          : null;
    }
  
  // auth change user stream
    Stream&lt;NayyaaUser?&gt; get userAuthStatus {
      return _authInstance.authStateChanges().map(_userFromFirebase);
    }
  
  
  // sign in with email + password
    Future signIn(String email, String password) async {
      try {
        UserCredential userAuthResult = await _authInstance
            .signInWithEmailAndPassword(email: email, password: password);
        User? user = userAuthResult.user;
        return _userFromFirebase(user!);
      } catch (e) {
        // ignore: avoid_print
        print(e.toString());
        return null;
      }
    }
  
  
    Future signUp(String email, String password) async {
      try {
        UserCredential userAuthResult = await _authInstance
            .createUserWithEmailAndPassword(email: email, password: password);
        User? user = userAuthResult.user;
        return _userFromFirebase(user);
      } catch (e) {
        print(e.toString());
        return null;
      }
    }
  
  // sign out
    Future signOut() async {
      try {
        return await _authInstance.signOut();
      } catch (e) {
        // ignore: avoid_print
        print(e.toString());
        return null;
      }
    }
  }
  </code></pre>
  <p>Here is firestore_service.dart</p>
  <pre><code>class FirestoreService {
    final CollectionReference _userRef =
        FirebaseFirestore.instance.collection('users');
  
    final CollectionReference _propertyRef =
        FirebaseFirestore.instance.collection('properties');
  
  
  //add or update user to firestore
    Future&lt;void&gt; saveUserDataToFirestore(NayyaaUser nayyaaUserData) {
      return _userRef.doc(nayyaaUserData.uid).set(nayyaaUserData.toMap());
    }
  
    // fetch user data from firestore
    Stream&lt;List&lt;NayyaaUser&gt;&gt; getNayyaaUser() {
      return _userRef.snapshots().map((snapshot) =&gt; snapshot.docs
          .map((document) =&gt;
              NayyaaUser.fromFirestore(document.data() as Map&lt;String, dynamic&gt;))
          .toList());
    }
  }
  </code></pre>