title: Flutter: BLoC emit states by listening to a stream of data
body: <p>I have a flutter app, which fetches a list of products from the server (product are related to the user, so the list doesn't change that much) so I'm saving the product lists locally. I'm using moor as a local database inside the application.
  when the user opens the products list page:</p>
  <ol>
  <li>I get the latest product creation date.</li>
  <li>Request data created after that date from the server async.</li>
  <li>I open a stream from the database to load the local data.</li>
  <li>When the the server response arrives with new data, I save it to the database, then the database stream will provide that data.</li>
  </ol>
  <p>the code:</p>
  <pre><code>Stream&lt;List&lt;Product&gt;&gt; getAllProducts() async* {
  this
      .fetchProductsOnline() // request data from server
      .then((value) =&gt; this.insertMultipleProducts(value)); // then save the received data locally
  yield* getLocalProductsList(); // meanwhile get the local data, and listen to new changes.
    }
  </code></pre>
  <p>then inside my BLoC I have to listen to that stream of data, and then emit states containing the data.
  but I don't seem to find the best/proper way to do that.</p>
  <p>what I tried:</p>
  <ol>
  <li><p>yield the stream of data after mapping it, and wrapping its items inside the state:</p>
  <pre><code>Stream&lt;ProductState&gt; mapProductListPageOpenedEventToState() async* {
      yield ProductListLoading(); 
      inProgress(); // enters the ui into a progress state
  
      yield* this
          .productRepository
          .getAllProducts()
          .map((event) =&gt; ProductListLoadedSuccess(event));
  
      await outProgress(); // exists the ui from the progress state
    }
  </code></pre>
  </li>
  </ol>
  <p>The problems I faced here are:
  - the code right under the stream yield, never gets executed (idk why).
  - it only work once, means if I exit that page and reopen it (trigger the load product even again), nothing would happen (no state change, no event triggered).</p>
  <ol start="2">
  <li><p>listen to the stream of data, and use the emit() method to emit new states.</p>
  <pre><code>Stream&lt;ProductState&gt; mapProductListPageOpenedEventToState() async* {
      yield ProductListLoading();
      inProgress(); // enters the ui into a progress state
      this.productRepository.getAllProducts().listen((event) {
        if (event.isNotEmpty) {
          emit(ProductListLoadedSuccess(event));
          outProgress();
         }
        });
    }
  </code></pre>
  </li>
  </ol>
  <p>this approach worked properly, I achieved what I wanted, but the docs say that the emit() method, should be used for test purposes only, and that states from bloc should be yield only.</p>
  <p>I'd appreciate any other solution that satisfies my needs, and that doesn't break any rules set by the docs.</p>