title: How to edit a user in flutter firestore
body: <p>I have been learning how does flutter work with firestore and now I am working in user auth with password, email and username, when a user is created the email and password are saved with an uid but the username and the email(again) are saved in firestore with a different uid, by the way I have tried a lot of things to make it have the same id but I currently cant find the way. in addition to this, there is also a function that is supposed to edit the username and save those changes. The problem comes when trying to implement the edit functinality because the edit form doesnt return anything as an output except the loading screen, I think this error is happening because of the uids. How can I fix this problem?</p>
  <p>models/user.dart</p>
  <pre><code>class CustomUser {
    final String uid;
  
    CustomUser({this.uid});
  }
  
  class UserData {
    final String uid;
    final String name;
  
    UserData({this.uid, this.name});
  }
  </code></pre>
  <p>models/username.dart</p>
  <pre><code>class Username {
    final String name;
  
    Username({this.name});
  }
  </code></pre>
  <p>services/auth.dart</p>
  <pre><code>class AuthService {
    final FirebaseAuth _auth = FirebaseAuth.instance;
  
  
    // create user obj based on fb user
    CustomUser _userFromFirebaseUser(User user) {
      return user != null ? CustomUser(uid: user.uid) : null;
    }
  
    Stream&lt;CustomUser&gt; get user {
      return _auth.authStateChanges().map(_userFromFirebaseUser);
    }
  
    //signin email password
    Future signInWithEmailAndPassword(String email, String password) async {
      try {
        UserCredential result = await _auth.signInWithEmailAndPassword(
            email: email, password: password);
        User user = result.user;
        return _userFromFirebaseUser(user);
      } catch (e) {
        print(e.toString());
        return null;
      }
    }
  
    //signup
    Future registerWithEmailAndPassword(String email, String password) async {
      try {
        UserCredential result = await _auth.createUserWithEmailAndPassword(
            email: email, password: password);
        User user = result.user;
        return _userFromFirebaseUser(user);
      } catch (e) {
        print(e.toString());
        return null;
      }
    }
  
    //signout
    Future signOut() async {
      try {
        return await _auth.signOut();
      } catch (e) {
        print(e.toString());
        return null;
      }
    }
  </code></pre>
  <p>services/database.dart</p>
  <pre><code>class DatabaseService {
    final String uid;
    DatabaseService({this.uid});
  
    final CollectionReference userCollection = FirebaseFirestore.instance.collection('usernames');
  
    Future updateUserData(String name) async {  // this is the function that has to edit the username
      return await userCollection.doc(uid).set({
        'name': name,
      });
    }
  
    Future uploadUserInfo(userMap) async {  // this function adds username and email to firestore
      return await userCollection.doc(uid).set(userMap);
    }
  
    List&lt;Username&gt; _usernameListFromSnapshot(QuerySnapshot snapshot) {
      return snapshot.docs.map((doc) {
        return Username(
          name: doc.data()['name'] ?? '',
        );
      }).toList();
    }
  
    // userData from snapshot
    UserData _userDataFromSnapshot(DocumentSnapshot snapshot) {
      return UserData(
        uid: uid,
        name: snapshot.data()['name'],
      );
    }
  
    Stream&lt;List&lt;Username&gt;&gt; get usernames {
      return userCollection.snapshots().map(_usernameListFromSnapshot);
    }
  
    Stream&lt;UserData&gt; get userData {
      return userCollection.doc(uid).snapshots().map(_userDataFromSnapshot);
    }
  }
  </code></pre>
  <p>register.dart(code that registers the user with a username)</p>
  <pre><code>onPressed: () async {
    if (_formKey.currentState.validate()) {
      setState(() =&gt; loading = true);
      dynamic result = await _auth.registerWithEmailAndPassword(email, password).then((val) {
        Map&lt;String, String&gt; userInfoMap = {
          &quot;name&quot;: name,
          &quot;email&quot;: email,
        };
        databaseService.uploadUserInfo(userInfoMap);
      });
      if (result == null) {
        setState(() {
          error = 'please suply a valid email';
            loading = false;
        });
      }
    }
  }),
  </code></pre>
  <p>editForm.dart</p>
  <pre><code>final _formKey = GlobalKey&lt;FormState&gt;();
  String _currentName;
  
  final user = Provider.of&lt;CustomUser&gt;(context);
  
  StreamBuilder&lt;UserData&gt;(
        stream: DatabaseService(uid: user.uid).userData,
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            UserData userData = snapshot.data;
            return Form(
                key: _formKey,
                child: Column(
                  children: &lt;Widget&gt;[
                    Text('edit username!'),
                    SizedBox(
                      height: 30,
                    ),
                    TextFormField(
                      // initialValue: userData.user gives a initial text to the input
                      validator: (val) =&gt; val.isEmpty ? 'Please enter a name' : null,
                      onChanged: (val) =&gt; setState(() =&gt; _currentName = val),
                    ),
                    RaisedButton(
                        child: Text('Save'),
                        onPressed: () async {
                          if (_formKey.currentState.validate()) {
                            print('update if good');
                            await DatabaseService(uid: user.uid).updateUserData(
                              _currentName ?? userData.name,
                            );
                          }
                          Navigator.pop(context);
                        })
                  ],
                ));
          } else {
            return Loading();
          }
        },
      );
  </code></pre>
  <p>If you have any questions please let me know;)</p>