Home > Net >  Flutter Fire Store - Assigning individual document fields to variables
Flutter Fire Store - Assigning individual document fields to variables

Time:12-03

I'm attempting to show a user's profile image on their home page by pulling the user's 'imageUrl' from their Fire Store document. I already have the app setup to where the user can upload a new image which updates the 'imageUrl' in Fire Store, but I don't know how to have the 'imageUrl' as a variable so I can show it on the app screen.

I've been reading documentation online but It seems over simplified or out of date. I've tried using StreamBuilder, but it pulls the data from every user in the database instead of for a single user. I just need to know how to pull this one value and use it as a variable in my dart code using "getString()" with a document reference or the collection reference I already have, thank you.

enter image description here

class _UserPageState extends State<UserPage> {


User user = auth.currentUser!;
  final CollectionReference collectionReference = FirebaseFirestore.instance.collection('users'); 

  // Get profileImageUrl from users userDoc
  String imageUrl = 'test'; // this should be the users imageUrl

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
            '${user.email}'), // this is being pulled from authentication not firestore
      ),
      body: Center(
        child: Column(
          children: [
// --------------------------- I tried using a stream builder here ---------------------
                StreamBuilder(
                  stream: collectionReference.snapshots(),
                  builder: (BuildContext context,
                      AsyncSnapshot<QuerySnapshot> snapshot) {
                    if (snapshot.hasError) {
                      return const Text(
                          'Something went wrong.'); // A: use incase the data does not load
                    }
                    final data = snapshot.requireData;
                    return ListView.builder(
                      shrinkWrap: true,
                      itemCount: data.size,
                      itemBuilder: (context, index) {
                        return Text(
                        // A: Stream builder will update with all of the users email addresses, I want this for one user exclusively
                            'My email is ${data.docs[index]['email']}'); 
                      },

CodePudding user response:

collection('users')
           .where("uid", isEqualTo: uid)
           .snapshots(),

To filter the data in firestore collection use "where". Store the user uid in offline and query it by where using the stored uid

CodePudding user response:

You can use the following function to get single data from stream.

 Stream<UserModel> getSingleStreamData({String? uId}) {
return ref!.where(CommonKeys.id, isEqualTo: uId).snapshots().map((value) => value.docs.first.data());}
  • Related