首页 > 解决方案 > Flutter - Flutter bloc 状态未更新

问题描述

我正在使用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正确使用更新状态?

标签: flutterblocflutter-bloc

解决方案


这可能会发生,因为你使用Equatable你的ItemState class并产生相同的状态,即IteamLoaded().

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


推荐阅读