使用 flutter_bloc 包时何时真正手动关闭一个 bloc?
When to actually close a bloc manually when using the flutter_bloc package?
我最近看了 felix Angelov 的 flutter_bloc 包 (https://pub.dev/packages/flutter_bloc) speech in youtube (https://www.youtube.com/watch?v=knMvKPKBzGE&t=3s),太棒了!
我对在 dispose 方法中手动关闭 bloc 有点困惑。我在演讲中听说如果我们使用flutter_bloc包,我们不必调用blocs的dispose方法,如果不对请指正。
提前致谢
当您将 BlocProvider
与构建器一起使用时,BlocProvider
负责创建和关闭 Bloc。在某些情况下,如果您想将现有的 bloc 提供给新路线,您应该使用 BlocProvider.value
代替,因为它不会自动关闭 bloc。
可以参考documentation
In most cases, BlocProvider should be used to create new blocs which will be made available to the rest of the subtree. In this case, since BlocProvider is responsible for creating the bloc, it will automatically handle closing the bloc.
In some cases, BlocProvider can be used to provide an existing bloc to a new portion of the widget tree. This will be most commonly used when an existing bloc needs to be made available to a new route. In this case, BlocProvider will not automatically close the bloc since it did not create it.
我最近看了 felix Angelov 的 flutter_bloc 包 (https://pub.dev/packages/flutter_bloc) speech in youtube (https://www.youtube.com/watch?v=knMvKPKBzGE&t=3s),太棒了!
我对在 dispose 方法中手动关闭 bloc 有点困惑。我在演讲中听说如果我们使用flutter_bloc包,我们不必调用blocs的dispose方法,如果不对请指正。
提前致谢
当您将 BlocProvider
与构建器一起使用时,BlocProvider
负责创建和关闭 Bloc。在某些情况下,如果您想将现有的 bloc 提供给新路线,您应该使用 BlocProvider.value
代替,因为它不会自动关闭 bloc。
可以参考documentation
In most cases, BlocProvider should be used to create new blocs which will be made available to the rest of the subtree. In this case, since BlocProvider is responsible for creating the bloc, it will automatically handle closing the bloc.
In some cases, BlocProvider can be used to provide an existing bloc to a new portion of the widget tree. This will be most commonly used when an existing bloc needs to be made available to a new route. In this case, BlocProvider will not automatically close the bloc since it did not create it.