title: Flutter + Bloc 6.0.6. BlocBuilder's "builder" callback isn't provided with the same state as the "buildWhen" callback
body: <p>I'm building a tic-tak-toe app and I decided to learn BLoC for Flutter along. I hava a problem with the <code>BlocBuilder</code> widget.</p>
  <p><strong>As I think about it</strong>. Every time Cubit/Bloc that the bloc builder widget listens to emits new state the bloc builder goes through this routine:</p>
  <ol>
  <li><p>Call <code>buildWhen</code> callback passing previous state as the <code>previous</code> parameter and <em>the newly emitted state</em> as the <code>current</code> parameter.</p>
  </li>
  <li><p>If the <code>buildWhen</code> callback returned true then rebuild.</p>
  </li>
  <li><p>During rebuilding call the <code>builder</code> callback passing given context as <code>context</code> parameter and <em>the newly emitted state</em> as <code>state</code> parameter. This callback returns the widget that we return.</p>
  </li>
  </ol>
  <p>So the conclusion is that the <code>current</code> parameter of the <code>buildWhen</code> call is always equal to the <code>state</code> parameter of the <code>builder</code> call. But in practice it's different:</p>
  <pre class="lang-dart prettyprint-override"><code>BlocBuilder&lt;GameCubit, GameState&gt;(
        buildWhen: (previous, current) =&gt; current is SetSlotSignGameState &amp;&amp; (current as SetSlotSignGameState).slotPosition == widget.pos,
        builder: (context, state) {
          var sign = (state as SetSlotSignGameState).sign;
          // Widget creation goes here...
        },
      );
  </code></pre>
  <p>In the <code>builder</code> callback, it throws:</p>
  <blockquote>
  <p>The following _CastError was thrown building BlocBuilder&lt;GameCubit, GameState&gt;(dirty, state:
  _BlocBuilderBaseState&lt;GameCubit, GameState&gt;#dc100):
  type 'GameState' is not a subtype of type 'SetSlotSignGameState' in type cast
  The relevant error-causing widget was:
  BlocBuilder&lt;GameCubit, GameState&gt;</p>
  </blockquote>
  <p>The method where I emit the states that is in the <code>GameCubit</code> class:</p>
  <pre class="lang-dart prettyprint-override"><code>// [pos] is the position of the slot clicked
  void setSlotSign(Vec2&lt;int&gt; pos) {
  
      // Some code
  
      emit(SetSlotSignGameState(/* Parameter representing the sign that is being placed in the slot*/, pos));
  
      // Some code
  
      emit(TurnChangeGameState());
  }
  </code></pre>
  <p><strong>Briefly about types of states</strong>. <code>SetSlotSignGameState</code> is emitted when a user taps on a slot in the tic-tac-toe grid and the slot is empty. So this state means that we need to change sign in some slot. <code>TurnChangeGameState</code> is emitted when we need to give the turn to the next player.</p>
  <p><strong>Temporary solution</strong>. For now I fixed it by saving the state from <code>buildWhen</code> callback in a private field of the <em><strong>widget's</strong> state</em> and then using it from the builder. <code>BlocListener</code> also has this problem but there I can just move the check from <code>listenWhen</code> callback into <code>listen</code> callback. The disadvantage of this solution is that it's very inelegant and inconvenient.</p>