简体   繁体   中英

Linking Firebase Authenticated user with 'users' collection in Flutter

New to flutter, and first post here!

I am building a mobile app with flutter, using Firebase and Cloud Firestore. Each user, when authenticated (by email and password), is also currently then added to a 'users' collection in Firestore - like this:

sign up method:

    Future signUp() async {
if (passwordConfirmed()) {
  await FirebaseAuth.instance.createUserWithEmailAndPassword(
    email: _emailController.text.trim(),
    password: _passwordController.text.trim(),
  );

  // add user details
  addUserDetails(
    _firstNameController.text.trim(),
    _lastNameController.text.trim(),
    _emailController.text.trim(),
    _baseStationController.text.trim(),
  );
}
}

Future addUserDetails(String firstName, String lastName, String email,
  String baseStation) async {
await FirebaseFirestore.instance.collection('users').add({
  'first name': firstName,
  'last name': lastName,
  'email': email,
  'base station': baseStation,
});

}

The signup() method is called when they input their information into text fields within a form, and click a button. This works successfully, and my user collection receives the data, and sign in/out works as it should.

Those users have a 'base station' (which is stored as a field within that users document in Firestore) which is their place of work. I want to show a welcome screen when they log in, so that the current users 'base station' is displayed.

I can access their email address using:

final thisUser = FirebaseAuth.instance.currentUser!;

and then for example to show this when they log in:

Text('Hello, ${thisUser.email!}')

this works no problem, however...

I can't work out how their authentication then links to the collection, so I want to show the 'base station' for the 'currentUser' for example when they log in (I don't want them to have to select their base station every time from a picker when they've already provided the information on sign up.

As an aside - I can also (separately) successfully read the data from the collection and (for example) create a listView builder with the users collection information, but this is for all users, and not specifically the 'currentUser' that is logged in.

All help gratefully received, apologies if I have missed anything.

update addUserDetails as follows    
 
 Future addUserDetails(String firstName, String lastName, String email,
              String baseStation) async {
            await FirebaseFirestore.instance.collection('users').doc(FirebaseAuth.instance.currentUser!.uid).set({
              'first name': firstName,
              'last name': lastName,
              'email': email,
              'base station': baseStation,
            });

if you then want to get specific user detail then use the code as follows

  final user = await FirebaseFirestore.instance.collection('users').doc(FirebaseAuth.instance.currentUser!.uid).get();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM