Flutter:我应何时处置BLoC? [英] Flutter: when should I dispose BLoC?

查看:86
本文介绍了Flutter:我应何时处置BLoC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应何时处置BLoC?

When should I dispose BLoC?

即使在官方论坛示例代码中,它为BLoC类创建dispose方法,但是它们从未被调用.

Even in the official bloc example code, it creates dispose methods for the BLoC class, but they never gets called.

非常感谢.

推荐答案

在您引用的特定示例中,BLoC无需手动处理,因为它需要在应用程序的整个生命周期中都可以访问,因此当应用被处置时,将被处置.在CartProvider中有此效果的注释.dart .

In the specific example you've referenced, the BLoC wouldn't need to be disposed of manually because it needs to be accessible throughout the lifetime of the app so will get disposed of when the app gets disposed. There is a comment to this effect in CartProvider.dart.

如果您的BLoC仅被应用程序的一部分使用,那么您绝对应该对其进行处置.对于您发布的示例,您可能希望将CartProvider包裹在StatefulWidget内,然后在该StatefulWidgetState对象的dispose方法中对其进行处理.

If your BLoC is only used by a portion of your app then you should definitely be disposing it. For the example you posted you would probably want to wrap the CartProvider inside a StatefulWidget and then dispose of it within the dispose method of that StatefulWidget's State object.

这篇关于Flutter:我应何时处置BLoC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆