How to retrieve "itemID" of a product in the Firebase?

Viewed 59

I can't get my product id.

When I click on the "favorites" button, the user's id is sent to the Firebase product sheet :

Firebase path

So I want to retrieve the user's id in the product sheet to display a colored Icon on the product sheet (in the application) if the user's name is in the Firebase database.

EX: "HVff3mXSTPT4bfKyiuKvOBZTYel1" is the name of the user who clicked on the "-Ms-PlyiwIZEm9V5kM8f" product sheet. So the icon should be colored for the user "HVff3mXSTPT4bfKyiuKvOBZTYel1"

So I need in my code to retrieve the product id (for the test my code I put the product id: "-Ms-PlyiwIZEm9V5kM8f").

So I want to replace that to retrieve the product id (if the user clicks on product 1 then it will display a color icon ONLY on product 1) :

@Override
void didChangeDependencies(){
   Firestore.intance.collection("appProducts").document("-Ms-PlyiwIZEm9V5kM8f").get().then((value) async{
      setState(() {
         fav = value['favorites']; 
      });
      FirebaseAuth auth = FirebaseAuth.intance;
      FirebaseUser user = await auth.currentUser();
      if(vaf.contains(user.uid)){
         setState(() {
            isLiked = true; 
         });
      } else {
         setState(() {
            isLiked = false; 
         });
      }
   });
   super.didChangeDependencies()
}
1 Answers
final Stream<QuerySnapshot> favItems = FirebaseFirestore.instance.collection('user').doc(FirebaseAuth.instance.currentUser?.email).collection('favourite').snapshots();

StreamBuilder(
        stream: favItems,
        builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
          if (!snapshot.hasData) {
            return Center(child: CircularProgressIndicator(),);
          }
          return ListView.builder(
            padding: EdgeInsets.zero,
            physics: ScrollPhysics(),
            shrinkWrap: true,
            itemCount: snapshot.requireData.size,
            itemBuilder: (context, i) {
              final data = snapshot.requireData.docs[i];
              return IconButton(onPressed: () {
                FirebaseFirestore.instance.collection("user").doc(
                    FirebaseAuth.instance.currentUser?.email).collection(
                    "favourite").doc(data.id).delete();
              }, icon: Icon(Icons.close));
            },
          );
        },
      ),  

by using data you can get particular product id by it's index

instead of favItems you can define your own stream

Related