I'm having an issue return a Stream to a StreamBuilder widget in flutter. I'm trying to access a custom class that is stored as a list in my users collection in firebase, but for some reason it continues to return an empty list.
Here is an example of my PhotoEntry custom class:
class PhotoEntry {
final Timestamp date;
final String fileUrl;
PhotoEntry({
@required this.date,
@required this.fileUrl,
});
Map<String, dynamic> toMap() => {
'date': date.millisecondsSinceEpoch,
'fileUrl': fileUrl,
};
PhotoEntry.fromMap(Map<String, dynamic> data)
: date = new Timestamp.fromMillisecondsSinceEpoch(data['date']),
fileUrl = data['fileUrl'];
PhotoEntry.initial()
: date = Timestamp.now(),
fileUrl = 'No Photo';
}
And here is the Stream that is giving me troubles. As I stated, it is returning an empty list:
Stream<List<PhotoEntry>> getUserPhotosStream(User user) {
if (user == null) {
return Stream.empty();
}
try {
return _db
.collection('users')
.document(user.uid)
.collection('photos')
.snapshots()
.map((list) => list.documents
.map((doc) => PhotoEntry.fromMap(doc.data))
.toList());
} catch (e) {
print(e);
rethrow;
}
}
And getUserPhotosStream is used here:
Widget _galleryPage() {
return StreamBuilder<List<PhotoEntry>>(
stream: _db.getUserPhotosStream(_user),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting ||
!snapshot.hasData) {
return Container(child: Center(child: CircularProgressIndicator()));
} else {
return ListView.builder(
scrollDirection: Axis.horizontal,
physics: BouncingScrollPhysics(),
itemCount: _user.photos.length,
itemBuilder: (context, index) {
var photo = snapshot; // For debugging
print(photo); // For dubigging
// return Card(photo)
},
);
}
},
);
}
I've verified the collection and document titles are correct, and the User being passed in is not null and contains all the correct data. What am I doing wrong here?
PhotoEntry.fromMapis called multiple times but you have an empty list? - pskinkPhotoEntry.fromMapis only called the one time while converting my map to a list. But yes, it's an empty list. - bricewagetUserPhotosStreammethod? post the code of yourStreamBuilder- pskinkStreamBuilder- bricewaitemCount: _user.photos.lengthis incorrect - you have to get the count based onsnaphot.data- pskink