简体   繁体   English

Flutter:provider 是 BLoC 模式的替代品吗?

[英]Flutter : Is provider an alternative to the BLoC pattern?

I know that BLoC in flutter acts like the viewmodel layer in android's MVVM, so the data does not gets fetched again and again upon configuration changes (for ex: change in screen orientation).我知道 flutter 中的 BLoC 就像 android 的 MVVM 中的 viewmodel 层,因此在配置更改时不会一次又一次地获取数据(例如:更改屏幕方向)。

I am confused if provider replaces the functionality of RxDart in BLoC pattern or it replaces the role BLoC pattern itself.我很困惑提供者是替换 BLoC 模式中 RxDart 的功能还是替换角色 BLoC 模式本身。

Also, if I don't use BLoC at all an only providers does the app survives configuration changes.此外,如果我根本不使用 BLoC,那么应用程序是否会在配置更改后幸存下来。

Please explain what are the limitations of provider over BLoC, RxDart combination with some use cases.请通过一些用例解释提供程序对 BLoC、RxDart 组合的限制。

Provider in itself doesn't replace the BLoC pattern. Provider 本身并不能取代 BLoC 模式。 However, you can set up your architecture to use Provider in a way that could replace the BLoC pattern.但是,您可以将架构设置为以一种可以替代 BLoC 模式的方式使用 Provider。

One way to do that would be to use the MVVM pattern, where you have a view model class that extends a ChangeNotifier .一种方法是使用 MVVM 模式,其中您有一个扩展 ChangeNotifier的视图模型类。 Then you can listen to that view model with a ChangeNotifierProvider so that the UI gets rebuilt any time the view model changes.然后,您可以使用ChangeNotifierProvider收听该视图模型,以便在视图模型更改时重建 UI。 FilledStacks does that well here . FilledStacks 在这里做得很好。

See also也可以看看

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

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