-1

I have a StreamBuilder that is connected to Firestore. It supplies a stream of type List<GameResults>. GameResults is a class that has a key called competitors, which is an array of Competitor objects, each of which has a key called competitorId, which is what I'd like to use to filter. When I call GameResults.fromMap in the gameStream below, I am assuming the resulting stream contains objects that I can use dot notation to access the object properties. When I try to filter the stream based on a specific value (a userId) inside the competitors array, I get an error that says: type '(dynamic) => dynamic' is not a subtype of type '(GameResults) => bool'. Can anyone help me understand where I'm going wrong?

 @override
  Widget build(BuildContext context) {
    final Topic topic = widget.topic;
    final firestore = Firestore.instance;

    Stream<List<GameResults>> gameStream = firestore
        .collection('gameResults')
        .where('topicId', isEqualTo: topic.topicId)
        .snapshots()
        .map((snapshot) {
      return snapshot.documents.map((doc) {
        return GameResults.fromMap(doc.data);
      }).toList();
    });

return StreamBuilder(
        stream: gameStream,
        builder: (context, snapshot) {
          final games = snapshot.data;
//below is the offending code:
          final myGame = games
              .where((game) => game.competitors
                  .where((competitor) => competitor.competitorId == userId))
              .toList()[0];
          

My Firestore data is

user3735816
  • 225
  • 2
  • 11

1 Answers1

1

UPDATE: After adding the return type List<GameResults> to the variable games in the streambuiler, I ended up implementing a method that ran a for loop to pull out the correct object from the stream. Below is the for loop:

            for (var game in games) {
              var competitors = game.competitors;
              var isMyGame = competitors.contains('competitorId' == userId);
              if (isMyGame != null) {
                return game;
              }
            }
          }

          final game = getMyGame(games);
user3735816
  • 225
  • 2
  • 11