简体   繁体   中英

How does different users get their own data in Flutter Firebase?

I'm new to Firebase and I managed to finish authentication in my flutter app using Firebase as cloud database and it works perfectly fine. But how do I get the current user data? My situation/problem goes something like this.

  • User A logged in and get username A(their name)
  • User B logged in and still get username A(User A name)

the situation is like I logged in Facebook but I entered someone else's profile because I read the same collection and document. This is my code of how it goes

CollectionReference users = FirebaseFirestore.instance.collection('Harris');

return FutureBuilder<DocumentSnapshot>(
        future: users.doc('username').get(),
        builder: (BuildContext context,
            AsyncSnapshot<DocumentSnapshot> snapshot) {
          if (snapshot.hasError) {
            return Center(child: Text("Something went wrong"));
          }

          if (snapshot.hasData && !snapshot.data!.exists) {
            return Center(child: Text("Document does not exist"));
          }

          if (snapshot.connectionState == ConnectionState.done) {
            Map<String, dynamic> data =
                snapshot.data!.data() as Map<String, dynamic>;
            return Container(
                height: double.infinity,
                width: double.infinity,
                decoration: BoxDecoration(
                  color: brightGray,
                ),
                child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Text(
                        'Welcome,',
                        style: ktextFont3,
                      ),
                      Text(
                        '${data['username']}',
                        style: ktextFont4,
                      ),]));}
return Center(
              child: CircularProgressIndicator(
            color: mintGreen,
          ));
        },
      );

this is my Firebase Console

Firebase 控制台

I have tried to replace the collection and document with username variable but it shows null when I hot restart the app. Any advice/assist is appreciated. Thank you

If userA signed in you can get name,email,firebase uid and image from this line of code

Firebase id: FirebaseAuth.instance.currentUser..uid

Firebase name: FirebaseAuth.instance.currentUser..displayName

Firebase email: FirebaseAuth.instance.currentUser..email

Firebase photo_url: FirebaseAuth.instance.currentUser..photoURL

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