Flutter – Flutter bloc state not updating

Issue

I’m using flutter_bloc library to manage the state of the widgets. I’m trying to update a variable in the state that is a List. When I capture the state and modify the desired value, then yield the new state, the state doesn’t update, but the clone of the state is changed.

Stream<ItemState> _updateRestrictions(ItemUpdateRestrictionValue event) async * {
  if (state is ItemLoaded) {
    final restrictions = (state as ItemLoaded).restrictions;

    final newState = restrictions.map((restriction) {
      if (restriction.id == event.restrictionId) {
        return ItemRestriction(
          id: restriction.id,
          name: restriction.name,
          restrictionType: restriction.restrictionType,
          restrictionValues: restriction.restrictionValues,
          restrictionValue: event.restrictionValueId
        );
      }

      return restriction;
    }).toList();

    yield ItemLoaded(restrictions: newState);
  }
}

Am I doing something wrong? Or how do you update the state using flutter_bloc correctly?

Solution

That probably happens because you use Equatable on your ItemState class and yield the same state back-to-back which is IteamLoaded().

You may wanna read this https://bloclibrary.dev/#/faqs?id=when-to-use-equatable

Answered By – Federick Jonathan

Answer Checked By – David Marino (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *