0

enter image description here

I have a usersData collection, it has a User.uid document in it, it has a Reminders collection. (in the screenshot) I'm trying to get the stream of this collection and output to Flutter. But one emptiness comes out.

final CollectionReference usersCollection = Firestore.instance.collection('usersData');

Geting collection

Stream<List<Reminder>> get reminders {
return usersCollection.document(uid).collection('reminders').snapshots().map(_remListFromSnapshot);}

Maping

List<Reminder> _remListFromSnapshot(QuerySnapshot snapshot){
//print(snapshot.toString());
return snapshot.documents.map((doc)
{
  print("Test " + doc.data['description']);
  return Reminder(
    description: doc.data['description'] ?? '',
  );
}).toList();
}

Widget for element

final Reminder reminder;
ReminderTile({this.reminder});

Widget build(BuildContext context) {
print("Here " + reminder.description);
return Padding(
  padding: EdgeInsets.only(top: 8.0),
  child: Card(
    margin: EdgeInsets.fromLTRB(20, 6, 20, 0),
    child: ListTile(
      leading: CircleAvatar(
        //backgroundImage: AssetImage('assets/coffee_icon.png'),
        radius: 25.0,
        backgroundColor: Colors.brown[300],
      ),
      title: Text(reminder.description),
    ),
  ),
);
}

And list widget

class RemList extends StatefulWidget {
@override
_RemListState createState() => _RemListState();
}

class _RemListState extends State<RemList> {
@override
Widget build(BuildContext context) {

final reminders = Provider.of<List<Reminder>>(context) ?? [];

return ListView.builder(
  itemCount: reminders.length,
  itemBuilder: (context, index) {
    return ReminderTile(reminder: reminders[index]);
  },
);
}
}
SuxoiKorm
  • 159
  • 2
  • 11

2 Answers2

0

What you should do is ...snapshots().listen(_remListFromSnapshot);

SempaiLeo
  • 334
  • 2
  • 8
0

You are missing the listen method.

You can use the listen method in the following way:

Stream<List<Reminder>> get reminders {
return usersCollection.document(uid).collection('reminders')
  .snapshots()
  .listen(_remListFromSnapshot);
}

Here is another Stackoverflow which states the same as previous.

Let me know if it works for you.

Nibrass H
  • 2,403
  • 1
  • 8
  • 14
  • No it's no work error: A value of type 'StreamSubscription' can't be returned from function 'reminders ' because it has a return type of 'Stream>'. – SuxoiKorm Jun 03 '20 at 23:16