felangel / bloc

A predictable state management library that helps implement the BLoC design pattern
https://bloclibrary.dev
MIT License
11.84k stars 3.4k forks source link

Transition is not happening if data that coming to the state is same as previous data. [Weather App Example] #902

Closed tijanirf closed 4 years ago

tijanirf commented 4 years ago

*Disclaimer: I'm relatively new to the Flutter and BloC world.

So, i tried weather app tutorial, it is working as intended. Now i try to change it to my custom api, the api's response is the same every time i request it (different with the weather app example). The initial load of the app working fine, but when i pull the list to refresh it, the loading indicator won't disappear.

I tried to debug it and i realized after i got response from api and passed it to the 'LoadedState', the transition is not happening. I suspect this cause the refresh completer not completed and the refresh indicator won't disappear. Can someone tell me how to handle the case when the api response is identical and make the refresh indicator disappear? or at least tell me which one is wrong with my current understanding.

Please enlighten me! Thank you.

felangel commented 4 years ago

Hi @tijanirf 👋 Thanks for opening an issue!

If you want bloc to trigger state changes even when the same state is yielded over and over then you can just change your state to not extend Equatable. Alternatively, you might want to add a lastUpdated timestamp to your state and each refresh would have a new timestamp (even if the data is the same from the API).

Hope that helps! 👍

tijanirf commented 4 years ago

Thanks @felangel for the fast response.

However, I still don't understand the implementation of the alternative solution (lastUpdated timestamp). Do you mean adding lastUpdated data on the API response so the response will be unique on every request?

Is there any project example on the site that implements this? Thanks in advance.

felangel commented 4 years ago

@tijanirf no problem! I mean add a lastUpdated to your state model (you don't have to change your API).

yield Loading();
try {
  final weather = await _repository.getWeather(...);
  yield WeatherLoaded(weather: weather, lastUpdated: DateTime.now());
} catch (
 ...
}