Flutter:我应何时处置BLoC?

编程入门 行业动态 更新时间:2024-10-25 14:25:25
本文介绍了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内,然后在该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?

本文发布于:2023-11-03 06:32:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1554462.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Flutter   BLoC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!