1
votes

I'm trying to map a stream from Firestore into a list in Flutter. I can retrieve the data successfully, but when I pass it to the StreamBuilder it receives null.

Stream<List<String>> getTeamMembers(String idSquadra) {
    var res = teamsCollection.where('idSquadra', isEqualTo: idSquadra).snapshots().map(_playerNamesFromSnapshot);
    return res;
  }

  List<String> _playerNamesFromSnapshot(QuerySnapshot snap) {
    DocumentSnapshot doc = snap.documents[0];
    var info = doc.data['infoGiocatori'];
    var res = info.map((i) => "${i['nome']} ${i['cognome']} (${i['displayName']})");
    print('res: $res');
    return res;
  }

Here res correctly shows as res: (Mario Rossi (MR1), Luigi Verdi (LV2)), but when I try to access it in the view, I get data: null

return StreamBuilder<List<String>>(
        stream: dbService.getTeamMembers(dbService.currentUser.idSquadra),
        builder: (context, snapshot) {
          print('snapshot: $snapshot');
          print('data: ${snapshot.data}');
          return Container();     
        });

EDIT: log for print snapshot:

snaphot: AsyncSnapshot<List<String>>(ConnectionState.active, null, type 'MappedListIterable<dynamic, dynamic>' is not a subtype of type 'List<String>')
1
add print('snapshot: $snapshot'); and post the logspskink
added the log @pskinkMerig
and print('error: ${snapshot.error}'); - everything should be clear nowpskink
error: type 'MappedListIterable<dynamic, dynamic>' is not a subtype of type 'List<String>' . @pskinkMerig
change to return info.map<String>((i) => '...').toList();pskink

1 Answers

1
votes

If anybody needs this I found the problem thanks to pskink's advices. I was missing a type cast and a toList() call.

Correct code:

return info.map<String>((i) => '...').toList();