BLoC:如何通过? [英] BLoC: how to pass it?

查看:69
本文介绍了BLoC:如何通过?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道通过集团的最佳方法。我读过有关bloc提供程序的信息,但是使用它们和只是在构造函数中传递bloc之间有什么区别?

I would like to know the best way to pass the bloc. I read about the bloc providers, but what`s the difference between using them and just passing the bloc in the constructor like:

ExampleView X = ExampleView(bloc,...)

实际上,我发现这种方式更易于测试,并且保持代码清洁的更好方法。例如,如果我有更多集团,可能会发生以下情况:

Actually I find this way easier to test and also a better way to keep the code cleaner. For example, if I have more blocs, something like this can happen:

XBlocProvider(
                  bloc: XBloc,
                  child: YBlocProvider(
                      bloc: Y,
                      child: ZBlocProvider...
                    )

或者可能只是我缺乏知识。
那么,有哪些好处?

or maybe it's just my lack of knowledge. So, which ones are the benefits?

推荐答案

此问题:

ExampleView X = ExampleView(bloc,...)

只有当您将您的集团用作带有某些 Stream\ValueNotifier 的普通类/提供者时,这才有意义。

It only make sense if you use your bloc as normal class/provider with some Stream\ValueNotifier. Otherwise it has some issues.

如果它是全局块,则传递它的方式更耗费精力,您应该在 XBlocProvider 上方使用 MaterialApp

If it's global bloc, it's more exhausting way to pass it. You should use XBlocProvider on top of MaterialApp.

顺便说一句,如果它是全局/顶级集团,则可以执行以下操作:

By the way, if it's global/top level bloc, you can do this:

XBlocProvider(
  bloc: xBloc, // Singleton
  child XPage,
...

这样,您可以从任何地方访问此bloc应用程序,也可以收听它。

This way, you can access this bloc from anywhere of the application and also you can listen it.

如果是本地团体,因为我们的收听方式是 Bloc ChangeNotifierProvider 通过 InheritedWidget updateShouldNotify 方法,以构造函数的形式传递没有任何意义,因为您不能按预期直接使用它。您需要将该实例放入 BlocProvider 内并再次使用它,因此这是额外的工作。

If it's local bloc, because the way we listen Bloc or ChangeNotifierProvider via InheritedWidget's updateShouldNotify method, it doesn't make sense to pass as constructor because you can't use it directly as you intended. You need to put that instance inside BlocProvider and consume it again, so it's extra work.

https://api.flutter.dev/flutter/widgets/InheritedWidget/updateShouldNotify.html

要克服多嵌套的 BlocProvider s,可以使用 MultiProvider MultiBlocProvider

To overcome multi nested BlocProviders you can use MultiProvider or MultiBlocProvider.

示例:

MultiBlocProvider(
  providers: [
    XProvider(...),
    YProvider(...),
    ZProvider(...),
  ],
  child: someWidget,
)

有多种传递方式,具体取决于您的需求,但不必担心 InheritedWidget ,因为这是获取您的 XBlocProvider 的非常便捷的方法。

There are multi ways to pass depends on your need but don't worry about InheritedWidget because it's pretty fast and convenient way to obtain your XBlocProvider.

最后,尝试为了理解每种方法,我特别建议您掌握这篇文章:

At the end, try to comprehend every approach, I especifically suggest you to grasp this article:

https://www.didierboelens。 com / 2018/12 / reactive-programming --- streams --- bloc --- practical-use-cases /

使用时您会明白的与提供者一起或作为单例或实例化(例如您的示例等)一起使用。

You'll get the idea when to use bloc with provider or as a singleton or instantiate like your example etc.

这篇关于BLoC:如何通过?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆