title: Dart/Flutter - "yield" inside a callback function
body: <p>I need to yield a list for a function; however, I want to yield the list from within a callback function, which itself is inside the main function - this results in the yield statement not executing for the main function, but rather for the callback function.</p>
  
  <p>My problem is very similar to the problem that was solved here: <a href="https://stackoverflow.com/questions/42228246/dart-component-how-to-return-result-of-asynchronous-callback">Dart Component: How to return result of asynchronous callback?</a> but I cannot use a Completer because I need to yield and not return.</p>
  
  <p>The code below should describe the problem better:</p>
  
  <pre><code>Stream&lt;List&lt;EventModel&gt;&gt; fetchEvents() async* { //function [1]
      Firestore.instance
          .collection('events')
          .getDocuments()
          .asStream()
          .listen((snapshot) async* { //function [2]
        List&lt;EventModel&gt; list = List();
        snapshot.documents.forEach((document) {
          list.add(EventModel.fromJson(document.data));
        });
  
        yield list; //This is where my problem lies - I need to yield for function [1] not [2]
      });
    }
  </code></pre>