Flutter-Flutter群组状态未更新

时间:2020-06-19 22:32:12

标签: flutter bloc 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更新状态?

1 个答案:

答案 0 :(得分:2)

之所以可能发生这种情况,是因为您在Equatable上使用了ItemState class并产生了相同的状态IteamLoaded()

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