Flutter - Flutter 集团状态未更新

Flutter - Flutter bloc state not updating

我正在使用 flutter_bloc 库来管理小部件的状态。我正在尝试更新状态为 List 的变量。当我捕获状态并修改所需的值,然后生成新状态时,状态不会更新,但状态的克隆已更改。

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);
  }
}

我是不是做错了什么?或者如何正确使用 flutter_bloc 更新状态?

这可能是因为您在 ItemState class 上使用 Equatable 并连续产生相同的状态,即 IteamLoaded()

您可能想阅读此内容https://bloclibrary.dev/#/faqs?id=when-to-use-equatable