title: Managing state for onBackPressed in Flutter bloc
body: <p>So I have a simple list that's clickable and goes to DetailScreen, issue I have is when I click back from the DetailScreen, how can I manage this state to save the last list?</p>
  
  <p>Bloc </p>
  
  <pre class="lang-dart prettyprint-override"><code>if (event is GetNews &amp;&amp; !_hasReachedMax(state)) {
        try {
          if (currentState is NewsInitial) {
            final news = await fetchNews(event.cat, pageNumber);
            yield NewsLoaded(news, false);
          }
          if (currentState is NewsLoaded) {
            pageNumber++;
            final news = await fetchNews(event.cat, pageNumber);
            yield news.isEmpty
                ? currentState.copyWith(hasReachedMax: true)
                : NewsLoaded(currentState.node + news, false);
          }
        } catch (error) {
          print(error);
          yield NewsError("Error fetching news" + error);
        }
      } else if (event is GetDetailedNews) {
        try {
          final filter = await fetchDetailedNews(event.id);
          yield DetailedNewsLoaded(filter);
        } catch (error) {
          yield NewsError("Couldn't fetch news : $error");
        }
      }
  </code></pre>
  
  <p>Attaching the event to the bloc</p>
  
  <pre class="lang-dart prettyprint-override"><code>  @override
    void initState() {
      super.initState();
      _postBloc = BlocProvider.of&lt;NewsBloc&gt;(context)
        ..add(GetNews(widget.cat));
    }
  </code></pre>
  
  <p>BlocBuilder</p>
  
  <p>OnBackPressed I'm just stick in the <code>else</code> since I don't know how to manage the state</p>
  
  <pre class="lang-dart prettyprint-override"><code>    return BlocBuilder&lt;NewsBloc, NewsState&gt;(builder: (context, state) {
        if (state is NewsLoaded) {
            return ListView.builder(
                controller: _scrollController,
                itemCount: state.hasReachedMax
                    ? state.node.length
                    : state.node.length + 1,
                itemBuilder: (context, index) {
                  fullList = state.node;
                  print("list: ${state.node} \nlength: ${state.node
                      .length} \nindex: $index \n--------------");
                  return index &gt;= state.node.length ?
                  BottomLoader() :
                  listViews(context, state.node[index], index);
                });
        }
        else if (state is NewsError) {
            return Center(
                child: Container(
                  child: Text(state.message),
                ));
        }
        else {
          return Center(child: CircularProgressIndicator(),);
        }
      });
  </code></pre>
  
  <p>States</p>
  
  <pre class="lang-dart prettyprint-override"><code>abstract class NewsState extends Equatable {
    const NewsState();
  
    @override
    List&lt;Object&gt; get props =&gt; [];
  }
  
  class NewsInitial extends NewsState {
    const NewsInitial();
  
    @override
    List&lt;Object&gt; get props =&gt; [];
  }
  
  class NewsLoading extends NewsState {
    const NewsLoading();
  
    @override
    List&lt;Object&gt; get props =&gt; [];
  }
  class NewsLoaded extends NewsState {
    final List&lt;Node&gt; node;
    final bool hasReachedMax;
  
    NewsLoaded(this.node, this.hasReachedMax);
  
    NewsLoaded copyWith({List&lt;Node&gt; node, bool hasReachedMax}) {
      return NewsLoaded(node ?? this.node, hasReachedMax ?? this.hasReachedMax);
    }
  
    @override
    List&lt;Object&gt; get props =&gt; [node];
  }
  
  class DetailedNewsLoaded extends NewsState {
    final List&lt;Node&gt; node;
  
    DetailedNewsLoaded(this.node);
  
    @override
    List&lt;Object&gt; get props =&gt; [node];
  }
  }
  </code></pre>
  
  <p>In the detail screen i add the GetDetailScreen event, and this event stays when onBackPressed</p>
  
  <pre class="lang-dart prettyprint-override"><code>  @override
    void initState() {
      BlocProvider.of&lt;NewsBloc&gt;(context)
        ..add(GetDetailedNews(widget.id));
      super.initState();
    }
  </code></pre>