skip to Main Content

In my Flutter app, I’m trying to get the data from a Document in Firestore. Here’s the data I want to get :

Firestore document’s data

I need to fetch that url from the Document. So far, when I needed to fetch data from a collection, I used a Streambuilder. But here I need to fetch data from a document, so I get this error message :

late Stream<DocumentSnapshot<Map<String, dynamic>>>? personnalData =
      FirebaseFirestore.instance
          .collection('Decembre')
          .doc(uid)
          .collection('Docs')
          .doc('test')
          .snapshots();

   StreamBuilder<QuerySnapshot>(
              stream: personnalData, // Error: The argument type 'Stream<DocumentSnapshot<Map<String, dynamic>>>?' can't be assigned to the parameter type 'Stream<QuerySnapshot<Map<String, dynamic>>>?'.
              builder: (BuildContext context,
                  AsyncSnapshot<QuerySnapshot> snapshot) {
                if (snapshot.hasError) {
                  return const Text('Something went wrong');
                }
                if (snapshot.connectionState == ConnectionState.waiting) {
                  return Center(child: CircularProgressIndicator());
                }
                return Stack(
                  children: snapshot.data!.docs
                      .map((DocumentSnapshot document) {
                        Map<String, dynamic> data =
                            document.data()! as Map<String, dynamic>;

                        return PageView.builder(
                                            controller: _controller,
                                            itemCount: 3,
                                            itemBuilder: (context, index) {
                                             
                                              return Container(
                                                child: InteractiveViewer(
                                                  minScale: 0.1,
                                                  maxScale: 4.0,
                                                  child: Image.network(
                                                // FETCH URL FROM DOCUMENT 'TEST'
                                                    width:
                                                        MediaQuery.of(context)
                                                            .size
                                                            .width,
                                                    fit: BoxFit.cover,
                                                    loadingBuilder: (context,
                                                        child,
                                                        loadingProgress) {
                                                      if (loadingProgress ==
                                                          null) {
                                                        return child;
                                                      } else {
                                                        return Center(
                                                          child:
                                                              
                                                   CircularProgressIndicator(),
                                                        );
                                                ),
                                              );
                                            }),
                                      ),
                                    ],
                                  ),
                                ),
                              );
                            },
                            child: Text('Open'));
                      })
                      .toList()
                      .cast(),
                );
              },
            ),

Any suggestions ?

4

Answers


  1. Chosen as BEST ANSWER

    I found the solution !

    The problem was that I was trying to fetch data from a DocumentSnapshot using a StreamBuilder<QuerySnapshot> instead of StreamBuilder<DocumentSnapshot>

    Here's how I solved it :

    late Stream<DocumentSnapshot<Map<String, dynamic>>> personnalData =
          FirebaseFirestore.instance
              .collection('Decembre')
              .doc(uid)
              .collection('Docs')
              .doc('test')
              .snapshots();
    
    StreamBuilder<DocumentSnapshot>(
            stream: personnalData,
            builder: (BuildContext context,
                  AsyncSnapshot<DocumentSnapshot> snapshot) {
                        if (snapshot.hasData) {  
                            return Text(snapshot.data!['url']);
                        }
                        return CircularProgressIndicator();
            }),
    
    

    This code is working well for me. I hope that will help somebody !


  2. Refer the following example:

    StreamBuilder<DocumentSnapshot>(
        stream:  FirebaseFirestore.instance.collection('Decembre').doc(uid).collection('Docs').doc('test').snapshots(),
        builder: (context, snapshot) {
            if (snapshot.hasData) {
            return ListView.builder(
                itemCount: snapshot.data!.docs.length,
                itemBuilder: (context, index) {
                    DocumentSnapshot doc = snapshot.data!.docs[index];
                    return Column(
                           children:[
                               Text(doc['url'])
                           );
                });
            } else {
            return Text("No data");
            }
        },
    )
    
    Login or Signup to reply.
  3. Maybe what you need is just to specify the type of the QuerySnapshot:

    late Stream<DocumentSnapshot<Map<String, dynamic>>> personnalData =  // like this FirebaseFirestore.instance
          .collection('Decembre')
          .doc(uid)
          .collection('Docs')
          .doc('test')
          .snapshots();
    

    because the snapshots() is a method that returns Stream<DocumentSnapshot<Map<String, dynamic>>>, and setting only Stream<DocumentSnapshot> will be considered as a different type, which throws the error.

    Login or Signup to reply.
  4. maybe you can try

     StreamBuilder<DocumentSnapshot<Map<String, dynamic>>>(
                  stream: personnalData,
                  builder: (BuildContext context,
                      AsyncSnapshot<QuerySnapshot> snapshot) {
                    if (snapshot.hasError) {
                      return const Text('Something went wrong');
                    }
                    if (snapshot.connectionState == ConnectionState.waiting) {
                      return Center(child: CircularProgressIndicator());
                    }
                    return Stack(
                      children: snapshot.data!.docs
                          .map((DocumentSnapshot document) {
                            Map<String, dynamic> data =
                                document.data()! as Map<String, dynamic>;
    
                            return PageView.builder(
                                                controller: _controller,
                                                itemCount: 3,
                                                itemBuilder: (context, index) {
                                                 
                                                  return Container(
                                                    child: InteractiveViewer(
                                                      minScale: 0.1,
                                                      maxScale: 4.0,
                                                      child: Image.network(
                                                    // FETCH URL FROM DOCUMENT 'TEST'
                                                        width:
                                                            MediaQuery.of(context)
                                                                .size
                                                                .width,
                                                        fit: BoxFit.cover,
                                                        loadingBuilder: (context,
                                                            child,
                                                            loadingProgress) {
                                                          if (loadingProgress ==
                                                              null) {
                                                            return child;
                                                          } else {
                                                            return Center(
                                                              child:
                                                                  
                                                       CircularProgressIndicator(),
                                                            );
                                                    ),
                                                  );
                                                }),
                                          ),
                                        ],
                                      ),
                                    ),
                                  );
                                },
                                child: Text('Open'));
                          })
                          .toList()
                          .cast(),
                    );
                  },
                ),
    

    in streamBuilder you can add <DocumentSnapshot<Map<String, dynamic>>> same as you create stream.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search