In my Flutter app, I’m trying to get the data from a Document in Firestore. Here’s the data I want to get :
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
I found the solution !
The problem was that I was trying to fetch data from a
DocumentSnapshot
using aStreamBuilder<QuerySnapshot>
instead ofStreamBuilder<DocumentSnapshot>
Here's how I solved it :
This code is working well for me. I hope that will help somebody !
Refer the following example:
Maybe what you need is just to specify the type of the
QuerySnapshot
:because the
snapshots()
is a method that returnsStream<DocumentSnapshot<Map<String, dynamic>>>
, and setting onlyStream<DocumentSnapshot>
will be considered as a different type, which throws the error.maybe you can try
in streamBuilder you can add <DocumentSnapshot<Map<String, dynamic>>> same as you create stream.