gpt4 book ai didi

flutter - 使用 flutter_bloc 包时,何时实际手动关闭 block ?

转载 作者:行者123 更新时间:2023-12-04 17:19:54 28 4
gpt4 key购买 nike

我最近在 youtube ( https://pub.dev/packages/flutter_bloc ) 上观看了 felix Angelov 的 flutter_bloc 包 (https://www.youtube.com/watch?v=knMvKPKBzGE&t=3s) 演讲,太棒了!
我对在 dispose 方法中手动关闭块有点困惑。我从演讲中听说如果我们使用 flutter_bloc 包,我们不必为 bloc 调用 dispose 方法,如果这是错误的,请纠正我。
提前致谢

最佳答案

当您使用 BlocProvider与 build 者,BlocProvider负责创建和关闭一个 Bloc。如果您想为新路线提供现有区块,您应该使用 BlocProvider.value相反,因为它不会自动关闭该集团。
您可以引用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.

关于flutter - 使用 flutter_bloc 包时,何时实际手动关闭 block ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66850638/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com