简体   繁体   English

如何监听状态属性的变化 Flutter 块模式

[英]How to listen to state properties changes Flutter bloc pattern

I'm developing a Flutter app and I faced the title's mentioned problem.我正在开发 Flutter 应用程序,但遇到了标题中提到的问题。

The BLoC manage the behaviour of a form with properties of a Person Model. BLoC 管理具有人员模型属性的表单的行为。

So my state class looks like this.所以我的状态类看起来像这样。

class SynchronizedForm extends Equatable {

  final PersonModel savedModel;

  final PersonModel actualModel;

  SynchronizedForm(this.savedModel, this.actualModel);

} 

Note : The PersonModel also extends Equatable.注意:PersonModel 还扩展了 Equatable。

I've added a Simple Delegate to the BLoC, just for me to know what is exactly happening during the application run.我已经向 BLoC 添加了一个简单的委托,只是为了让我知道在应用程序运行期间到底发生了什么。

The delegate print on all state transitions.委托在所有状态转换上打印。

The properties of the actualModel are updated via Events (BLoC pattern).实际模型的属性通过事件(BLoC 模式)更新。

class PersonBloc extends Bloc<PersonEvent, PersonState> {

  PersonModel person;

  PersonBloc (PersonModel person);

  @override
  PersonState get initialState => SynchronizedForm (person, person.copyWith());

  @override
  Stream<PersonState> mapEventToState(
    PersonEvent event) async* {

    if (event is ModifyPerson)
      yield* _modifyPerson();

    else if ...
  }

  Stream<PersonState> _modifyPerson(
    ModifyPerson event) async* {

    PersonModel actual = state.actualModel;

    actual.prop = event.value;

    yield SynchronizedForm (
       state.savedModel,
       actual);
   }
}

So the main problem is that I have widgets in my screens that consumes this state changes, but when the actual state is SynchronizedForm and I modify the person I'm again returning a SynchronizedForm.所以主要问题是我的屏幕中有小部件会消耗这种状态变化,但是当实际状态是 SynchronizedForm 并且我修改了这个人时,我又返回了一个 SynchronizedForm。 There's no explicit transition here but I need one to.这里没有明确的过渡,但我需要一个。

I tryied without Equatable, with Equatable.我在没有 Equatable 的情况下尝试了 Equatable。 Using BlocConsumer, BlocListener (all this from flutter_bloc package).使用 BlocConsumer、BlocListener(所有这些都来自 flutter_bloc 包)。 I don't know what more to do.我不知道还要做什么。

I hope you understand what I'm trying to do.我希望你明白我在做什么。 Thank you all!谢谢你们!

From what I understand, you want to basically rebuild the state even though you're passing the same state.据我了解,即使您正在通过相同的状态,您也希望基本上重建状态。 For that you'll need to create new copies of the properties of the state so it catches the change.为此,您需要创建状态属性的新副本,以便捕获更改。

More on it here: https://bloclibrary.dev/#/faqs更多相关信息: https : //bloclibrary.dev/#/faqs

Have you tried to override props property of your model ?您是否尝试覆盖模型的props属性?

From equatable code documentation:来自等价代码文档:

  /// Equatables override their own `==` operator and [hashCode] based on their
  /// `props`.

So your model should look like this:所以你的模型应该是这样的:

class SynchronizedForm extends Equatable {

  final PersonModel savedModel;

  final PersonModel actualModel;

  // Add this 
  @override
  List<Object> get props => [savedModel, actualModel];

  SynchronizedForm(this.savedModel, this.actualModel);

} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM