Bloc: how to load same state twice ?

Created on 31 Dec 2019  路  1Comment  路  Source: felangel/bloc

hi,

i am first getting data from cache and then updating data from network. using folowing code but its not working. can anyone guide ?

```
@override
Stream mapEventToState(PostEvent event) async* {
final currentState = state;
if (event is Fetch && !_hasReachedMax(currentState)) {
try {
if (currentState is PostUninitialized)
{
try {
final posts = await _fetchPosts(0, cache: true);
yield PostLoaded(posts: posts, hasReachedMax: false,type:"cache");
}
catch(error,stacktrace)
{

        print("Exception occured: $error stackTrace: $stacktrace");
      }

      final posts1 = await _fetchPosts(0);
      yield PostLoaded(posts: posts1, hasReachedMax: false,type:"network");

      return;
    }

    if (currentState is PostLoaded)
    {
      final posts = await _fetchPosts((currentState.posts.length/20).toInt(),);
      yield posts.isEmpty
          ? currentState.copyWith(hasReachedMax: true)
          : PostLoaded(
              posts: currentState.posts + posts,
              hasReachedMax: false,type: "network"
            );
    }
  } catch (_) {
    //yield PostError();
  }
}
else if(event is Refresh)
{
  yield PostUninitialized();
  range = "";
  result= "";
  status = "";
  opponentId = "";
  final posts = await _fetchPosts(0);
  yield PostLoaded(posts: posts, hasReachedMax: false,type:"network");
  return;
}

else if(event is Filter && currentState is PostLoaded)
{

  yield PostUninitialized();
  final posts = await _fetchPosts(0);
  yield PostLoaded(posts: posts, hasReachedMax: false,type:"network");
  return;
}

}
```

question

Most helpful comment

Hi @sohampandya00 馃憢
Thanks for opening an issue!

Bloc ignores state changes when the emitted state is equal to the current state. If your state class is extending Equatable, then even if you yield a new state instance the state might be ignored if the values are the same as those on the current state. For cases where you want multiple states with the same values to trigger state changes you should avoid extending Equatable. Hope that helps 馃憤

>All comments

Hi @sohampandya00 馃憢
Thanks for opening an issue!

Bloc ignores state changes when the emitted state is equal to the current state. If your state class is extending Equatable, then even if you yield a new state instance the state might be ignored if the values are the same as those on the current state. For cases where you want multiple states with the same values to trigger state changes you should avoid extending Equatable. Hope that helps 馃憤

Was this page helpful?
0 / 5 - 0 ratings

Related issues

timtraversy picture timtraversy  路  3Comments

clicksocial picture clicksocial  路  3Comments

shawnchan2014 picture shawnchan2014  路  3Comments

MahdiPishguy picture MahdiPishguy  路  3Comments

abinvp picture abinvp  路  3Comments