简体   繁体   English

每当我使用 flutter_bloc 关闭我的应用程序时如何重置为初始状态/值

[英]How to reset to the initial state/value whenever I close my app using flutter_bloc

I am still a beginner when it comes to using flutter_bloc .在使用flutter_bloc时,我仍然是初学者。

I have tried flutter_bloc and curious how to reset my bloc class to its initial value when I have closed the page.我尝试过flutter_bloc并且很好奇如何在关闭页面时将我的 bloc 类重置为其初始值。

my_bloc_class.dart my_bloc_class.dart

class sumtotal_detail_transactionsbloc extends Bloc<String, String>{
    @override
    String get initialState => '0';

    @override
    Stream<String> mapEventToState(String sumtotal_detail_transactions) async* {
        yield sumtotal_detail_transactions.toString();
    }
}

My widget with a BlocBuilder .我的小部件带有BlocBuilder

BlocBuilder<sumtotal_detail_transactionsbloc, String>(
    builder: (context,sumtotal_detail_transactions) => Text(
        sumtotal_detail_transactions,style: TextStyle(
                fontSize: 12,
                color: Colors.brown[300]
            ),
        )
),

Whenever I close the page or navigate to the page, how can I always/automatically reset the sumtotal_detail_transactions back to its initial value?每当我关闭页面或导航到页面时,如何始终/自动将sumtotal_detail_transactions重置为其初始值?

It will break my app if the value is always kept/store as it is.如果值始终保持/存储原样,它将破坏我的应用程序。

Hey 👋 I would recommend providing the bloc in the page so when the page is closed the bloc is disposed automatically by BlocProvider.嘿👋 我建议在页面中提供 bloc,以便在页面关闭时 bloc 由 BlocProvider 自动处理。 No need to have a reset event, just make sure to scope blocs only to the part of the widget tree that needs it.不需要重置事件,只需确保将 blocs 仅作用于需要它的小部件树部分。 Hope that helps!希望有帮助!

As mentioned by the plugin author here ,正如插件作者在这里提到的,

I don't think it's a good idea to introduce a reset() because it directly goes against the bloc library paradigm: the only way to trigger a state change is by dispatching an event.我认为引入 reset() 不是一个好主意,因为它直接违背了 bloc 库范式:触发状态更改的唯一方法是调度事件。

With that being said, you must add an event/state the will be used to trigger an initialisation event.话虽如此,您必须添加将用于触发初始化事件的事件/状态。

For example:例如:

Add an initialisation event.添加初始化事件。

some_page_bloc_events.dart some_page_bloc_events.dart

class InitializePageEvent extends SomePageEvent {
  // You could also pass on some values here, if they come from the UI/page of your app
  @override
  String toString() => 'InitializePageEvent';
}

Add an initialisation state.添加初始化状态。

some_page_bloc_states.dart some_page_bloc_states.dart

class InitializePageState extends SomePageState {
  @override
  String toString() => 'InitializePageState';
}

Next, utilise these inside your bloc class to filter incoming events and notify the UI with the appropriate states.接下来,在您的 bloc 类中利用这些来过滤传入的事件并用适当的状态通知 UI。

some_page_bloc.dart some_page_bloc.dart

@override SomePageState get initialState => InitializePageState();

@override
Stream<SomePageState> mapEventToState(SomePageEvent event) async* {
    try {
        if(event is InitializePageEvent) {
            // Do whatever you like here
            yield InitializePageState();
        }
    } catch (e) {
        ...
    }
}

Finally, you can invoke the initialisation event wherever you deemed in necessary.最后,您可以在任何您认为必要的地方调用初始化事件。 In your case, it should be on the initState() method of your screen.在您的情况下,它应该在屏幕的initState()方法上。

some_page.dart some_page.dart

@override
void initState() {
  super.initState();
  _someTransactionBloc.dispatch(InitializePageEvent());
}

Felix provided a well-written documentation for his plugin, I suggest that you go over the intro concepts how BLoC works. Felix 为他的插件提供了一份写得很好的文档,我建议你复习一下 BLoC 工作原理的介绍性概念。 Please read it here .在此处阅读。

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

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