skip to Main Content

I want to retrieve All Add_product data using current user uid How is it possible.

My code return only loading widget because my query is incorrect. How can I write my query in a correct way?

My database:

123

My code

StreamBuilder(
                stream: FirebaseFirestore.instance.collectionGroup("Add_product").where("id",isEqualTo: FirebaseAuth.instance.currentUser!.uid).snapshots(),
                builder:(BuildContext context,AsyncSnapshot<QuerySnapshot>snapshot){
                  if (!snapshot.hasData) {
                    return Center(child: LoadingAnimationWidget.staggeredDotsWave(color: Colors.red, size: 10));
                  }

      `            return Container(
                   
                          child: InkWell(
                            child: Card(
                              elevation:5,
                              shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(15.0),
                              ),
                              child: Column(
                                children: [
                                  Padding(
                                    padding: const EdgeInsets.all(9.0),
                                    child: Image.network(snapshot.data!.docs[index]["url"],width: MediaQuery.of(context).size.width*.49,height:MediaQuery.of(context).size.height*.17 ,),
                                  ),
                                  SizedBox(height: 7,),
                                  Text(
                                    snapshot.data!.docs[index]["product_name"],
                                    style: TextStyle(
                                      fontWeight: FontWeight.bold,
                                      fontSize: 16,
                                    ),
                                    textAlign: TextAlign.center,
                                  ),
                                  SizedBox(height: 4),
               
                        );
                      },
                    ),
                  );
                }
            ),

2

Answers


  1. Try this one

    StreamBuilder(
                    stream: FirebaseFirestore.instance.collection("Add_product").snapshots(),
                    builder:(BuildContext context,AsyncSnapshot<QuerySnapshot>snapshot){
                      if (!snapshot.hasData) {
                        return Center(child: LoadingAnimationWidget.staggeredDotsWave(color: Colors.red, size: 10));
                      }
    
          `            return Container(
                       
                              child: InkWell(
                                child: Card(
                                  elevation:5,
                                  shape: RoundedRectangleBorder(
                                    borderRadius: BorderRadius.circular(15.0),
                                  ),
                                  child: Column(
                                    children: [
                                      Padding(
                                        padding: const EdgeInsets.all(9.0),
                                        child: Image.network(snapshot.data!.docs[index]["url"],width: MediaQuery.of(context).size.width*.49,height:MediaQuery.of(context).size.height*.17 ,),
                                      ),
                                      SizedBox(height: 7,),
                                      Text(
                                        snapshot.data!.docs[index]["product_name"],
                                        style: TextStyle(
                                          fontWeight: FontWeight.bold,
                                          fontSize: 16,
                                        ),
                                        textAlign: TextAlign.center,
                                      ),
                                      SizedBox(height: 4),
                   
                            );
                          },
                        ),
                      );
                    }
                ),
    

    If you also need user id, you can get the userId first and store it into the variable and pass that variable into where

    Login or Signup to reply.
  2. In my perspective the firebase structure which you are following is not recommended, consider changing it.

    To answer your question, you can try:

     @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Center(
            child: StreamBuilder<QuerySnapshot>(
              stream: FirebaseFirestore
                      .instance.
                      .collection('Add_product')
                      .doc(FirebaseAuth.instance.currentUser!.uid)
                      .collection('Add_product')
                      .snapshots(), 
              builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
                if (snapshot.hasError) {
                  return const Text('Something went wrong');
                }
                if (snapshot.connectionState == ConnectionState.waiting) {
                  return const Text("Loading");
                }
                return ListView(
                    children: snapshot.data!.docs.map((DocumentSnapshot document) {
                  Map<String, dynamic> data =
                      document.data()! as Map<String, dynamic>;
                  return ListTile(
                    title: Text(data['product_name']), // Should print "Cloth"
                  );
                }).toList());
              },
            ),
          ),
        );
      }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search