0
votes

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?

1
so PhotoEntry.fromMap is called multiple times but you have an empty list? - pskink
@pskink PhotoEntry.fromMap is only called the one time while converting my map to a list. But yes, it's an empty list. - bricewa
where do you use getUserPhotosStream method? post the code of your StreamBuilder - pskink
@pskink Edited my original post to include the StreamBuilder - bricewa
seems that itemCount: _user.photos.length is incorrect - you have to get the count based on snaphot.data - pskink

1 Answers

0
votes

I have come through a similar situation, not sure if this is exactly your case , but my answer could help people facing the empty list problem. In my case, there is a users collection inside of it there's a doctors collection which holds their messages collection. my messages collection

I added a doctor object manually through the firebase dashboard as a field, therefore firebase wasn't able to access it a collection , and was always returning empty list. the following image may help understand better: the right way to add dummy data