title: Flutter BlocProvider consumption
body: <p>I'm implementing a BLoC pattern for state management in my Fluter application. As I'm new in Flutter and BLoC particularly I'm evolving its usage gradually.
  For new I use BLoC to communicate between two pages. One page sends an asset to the BLoC and navigates to details page. The details page uses <code>StreamBuilder</code> to read from the BLoC and build page with according data:</p>
  <p>AppWidget:</p>
  <pre><code>Widget build(BuildContext context) =&gt; MultiProvider(
            providers: [
              BlocProvider(create: (context) =&gt; AssetBloc())
  ...
  </code></pre>
  <p>Requesting page</p>
  <pre><code>  _onAssetMenuAction(BuildContext context, AssetMenu value, Asset asset) {
      switch (value) {
        case AssetMenu.validate:
          var bloc = BlocProvider.of&lt;AssetBloc&gt;(context);
          bloc.validate(asset);
          Navigator.push(context,
              MaterialPageRoute(builder: (context) =&gt; ValidateAssetPage()));
          break;
      }
  </code></pre>
  <p>Validation page</p>
  <pre><code>  Widget build(BuildContext context) {
      var bloc = BlocProvider.of&lt;AssetBloc&gt;(context);
      Logger.root.info(&quot;Building validation page&quot;);
      return StreamBuilder&lt;AssetValidation&gt;(
          stream: bloc.outValidation,
          builder: (context, snapshot) =&gt; snapshot.hasData
              ? QrImage.withQr(qr: snapshot.data!.qr)
              : Text(&quot;No QR&quot;));
    }
  </code></pre>
  <p>BLoC</p>
  <pre><code>class AssetBloc extends BlocBase {
    //
    // Stream to handle the validation request outcome
    //
    StreamController&lt;AssetValidation&gt; _validationController =
        StreamController&lt;AssetValidation&gt;.broadcast();
    StreamSink&lt;AssetValidation&gt; get _inValidation =&gt; _validationController.sink;
    Stream&lt;AssetValidation&gt; get outValidation =&gt; _validationController.stream;
  
    //
    // Stream to handle the validation request
    //
    StreamController&lt;Asset&gt; _validateController = StreamController&lt;Asset&gt;();
    void Function(Asset) get validate =&gt; _validateController.sink.add;
  
    //
    // Constructor
    //
    AssetBloc([state]) : super(state) {
      _validateController.stream.listen(_handleLogic);
    }
  
    void _handleLogic(Asset asset) {
      _inValidation.add(AssetValidation.create(asset));
      Logger.root.finest(&quot;AssetValidation instance is sent to stream&quot;);
    }
  
    void dispose() {
      _validateController.close();
      _validationController.close();
    }
  }
  </code></pre>
  <p>The problem I have is I'm getting &quot;No QR&quot;. According to logs I see following sequence of actions:</p>
  <blockquote>
  <p>new AssetValidation.create(): Validating asset Instance of 'Asset'</p>
  </blockquote>
  <blockquote>
  <p>AssetBloc._handleLogic(): AssetValidation instance is sent to stream</p>
  </blockquote>
  <blockquote>
  <p>ValidateAssetPage.build(): Building validation page</p>
  </blockquote>
  <p>So at the moment of validation page building the validation result data should be in the stream but it seems they are not.</p>
  <p>Unit tests of <code>AssetBloc</code> work as expected. So I suspect it should be related to <code>StreamBuilder</code> in validation page.</p>