简体   繁体   English

在 Flutter 中使用 BLoC - 在有状态小部件与无状态小部件中的使用

[英]Using BLoC in Flutter - Usage in Stateful widgets vs Stateless Widgets

While using BLoC in flutter, whats the difference between defining the BlocBuilder / BlocListener in A Stateful widgets vs a Stateless widget?在 flutter 中使用 BLoC 时,在有状态小部件与无状态小部件中定义 BlocBuilder / BlocListener 有什么区别?

Can't we always use Stateless widgets as bloc takes care of rebuilding the widget for us?我们不能总是使用无状态小部件,因为 bloc 会为我们重建小部件吗? Are there any usecases where you might want to use a Stateful widget instead?是否存在您可能想要使用有状态小部件的用例?

PS: I am experimenting with the flutter_bloc 1.0.0 package for flutter ( https://pub.dev/packages/flutter_bloc ). PS:我正在试验用于 flutter 的flutter_bloc 1.0.0 package( https://pub.dev/packages/flutter_bloc )。

You can use stateless weights throughout the app as any rebuilding can be handled by the builder methods in StreamBuilder or BlocBuilder.您可以在整个应用程序中使用无状态权重,因为任何重建都可以由 StreamBuilder 或 BlocBuilder 中的构建器方法处理。 There is no requirement to do so, though it is generally recommended, but it might make sense to make some small widget stateful if you want a click to toggle some information or display the button that interacts with the bloc.尽管通常建议这样做,但没有要求这样做,但如果您希望单击以切换某些信息或显示与块交互的按钮,则使一些小部件有状态可能是有意义的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM