我应何时处置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内,然后在该StatefulWidget的State对象的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?
发布评论