0
votes

Please help me understand, why this code not working! I try to get data from a Stream (Firestore), and take this data to a list. I want to wait until the list is ready, and with this list do something. But .then or .whenComplete fires before the list is ready... This is the function to make the list and return it:

Future<List<EventDistance>> getEventsDistanceList(String eventId) async{
  Stream<FS.QuerySnapshot> qs =  EventDistanceDataRepository().getStreamByEventId(eventId: eventId);
  List<EventDistance> dList = [];
  EventDistance eventDistance;
   qs.forEach((document)  {
     document.forEach((docs) {
      eventDistance = eventDistanceFromJson(docs.data());
      dList.add(eventDistance);
      print(eventDistance.Name); //(3.) only for testing, to see if docs is not empty
    }
    );

  });
  print('return'); //(1.) only for testing, to see when return is fired
  return dList;
}

(return also fires before) i use this code so:

Future<List<EventDistance>> dList = getEventsDistanceList(filteredList[index].id );
dList.then((value) {
  print('value: $value'); //(2.) only for testing,to see if the returned list is empty or not (empty :-( )
  doSomething;
});

When i run, i recive first 'return' (1.), then 'value: null' (2.) (and an empty list) and then the elements of the list (Name1, Name2 ...) (3.). What do i wrong? How to wait to receive the list first? Thanks for the answeres!

3

3 Answers

0
votes

You need to use await in asynchronous functions. I'm guessing

Stream<FS.QuerySnapshot> qs =  
EventDistanceDataRepository().getStreamByEventId(eventId: eventId);

Should be

Stream<FS.QuerySnapshot> qs = await 
EventDistanceDataRepository().getStreamByEventId(eventId: eventId);

Where ever the operation that takes a long time happens gets the await keyword. Try the code labs to get better with async await

0
votes

works fine! the final code is:

final List<EventDistance> listOfEvents = document.docs.map((e) => eventDistanceFromJson(e.data())).toList();
0
votes

To become more confident with async operations read the perfect article by Didier Boelens

Let check what is going on in your code

Your getEventsDistanceList() routine is pure synchronous - all of it's content runs synchronously step by step

  1. synchronously subscribe to a Stream in qs.forEach and set callback listener (document) { ... } which will be fired on each stream item somewhere in future
  2. synchronous call print('return') is fired
  3. finally getEventsDistanceList() returns
  4. you listen to this Future returned from getEventsDistanceList() until it complete and then then() is fired with call to print('value: $value')
  5. first stream item is received and callback fired with print(eventDistance.Name)

5th step will repeat with new items until stream completes or ended with error (see Stream.forEach implementation)

I supposed you need only first Stream item (if not, do not hesistate reach me in comments)

If so rewrite your code

EventDistanceDataRepository()
  .getStreamByEventId(eventId: eventId)
  .first
  .then((document) => document.map((docs) => eventDistanceFromJson(docs.data())).toList())
  .then((value) { doSomething;});

I prefer more readable await notation

final FS.QuerySnapshot document = await EventDistanceDataRepository()
  .getStreamByEventId(eventId: eventId)
  .first;

final List<EventDistance> listOfEvents = document.docs.map((e) => eventDistanceFromJson(e.data())).toList();

doSomething with this list