简体   繁体   English

什么是颤振中的 BLOC 模式?

[英]What is BLOC patten in flutter?

I'm trying to understand the bloc pattern but unable to understand.我试图理解集团模式,但无法理解。 Browsed many tutorial but couldn't understood properly.浏览了很多教程,但无法正确理解。 I just understood that我才明白

  1. bloc pattern is used to managing the state of app. bloc 模式用于管理应用程序的状态。 and

  2. for take input, use sink and for output, we use Stream by stream controller.对于输入,使用接收器,对于输出,我们通过流控制器使用 Stream。

and not sure I'm getting wrong or right.并且不确定我是对还是错。

But clear thing is that, it's not enough for acquaintance of bloc pattern.但很明显,仅仅了解集团模式是不够的。

Explain it with simple language.用通俗的语言解释一下。

The Business Logic Component (BLoC) pattern is a pattern created by Google for state management.The BLoC pattern uses Reactive Programming to handle the flow of data within an app.业务逻辑组件 (BLoC) 模式是 Google 创建的用于状态管理的模式。 BLoC 模式使用响应式编程来处理应用程序内的数据流。

  1. A BLoC stands as a middleman between a source of data in your app (eg an API response) and widgets that need the data. BLoC 是您应用中的数据源(例如 API 响应)和需要数据的小部件之间的中间人。
  2. A BLoC has two simple components: Sinks and Streams. BLoC 有两个简单的组件:Sinks 和 Streams。 Input of BLoC is sink and output is stream. BLoC 的输入是接收器,输出是流。
  3. User inputs are given to the BLoC by adding data to the sink --> business logic is in the bloc which processes data --> Widgets listening to the streams will be notified with the processed data.通过向接收器添加数据,将用户输入提供给 BLoC --> 业务逻辑在处理数据的 bloc 中 --> 侦听流的小部件将收到处理数据的通知。
  4. In other words, it receives streams of events/data from the source, handles any required business logic and publishes streams of data changes to widgets that are interested in them.换句话说,它从源接收事件/数据流,处理任何所需的业务逻辑并将数据更改流发布到对它们感兴趣的小部件。

Here is a wonderful article that has an example that you can refer to understand BLoC better. 这是一篇很棒的文章,里面有一个例子,你可以参考它来更好地理解 BLoC。

BLoC can be implemented using StreamBuilder and StreamController but you should use flutter_bloc flutter package to cut the boilerplate code. BLoC 可以使用StreamBuilderStreamController来实现,但你应该使用flutter_bloc flutter 包来削减样板代码。 examples .例子

I hope this helps, in case of any doubts please comment.希望对大家有帮助,如有疑问,欢迎留言。 If this answer helps you then please accept and up-vote it.如果这个答案对您有帮助,请接受并投票。

  • Bloc makes it easy to separate presentation from business logic, making your code fast, easy to test, and reusable. Bloc 可以轻松地将表示与业务逻辑分开,使您的代码快速、易于测试和可重用。
  • Bloc attempts to make state changes predictable by regulating when a state change can occur and enforcing a single way to change state throughout an entire application. Bloc 试图通过调节何时发生状态更改并在整个应用程序中强制执行一种更改状态的单一方法来使状态更改可预测。

You can read more here , it is easy to understand你可以在这里阅读更多,很容易理解

it is a way to communicate between your logic and UI and change the UI base on your logic or vice versa in flutter, you will need to change your widget's (User interface) base on the some logic for example if user clicks on the A show the B text the or vice versa for example if year is 2020 show 2020 text to user bloc is just a pattern that helps you to accomplish this (whit some positive and some negative points) and there are lots of patterns other than bloc that will do this job for you这是一种在您的逻辑和 UI 之间进行通信并根据您的逻辑更改 UI 的方式,反之亦然,您将需要根据某些逻辑更改您的小部件(用户界面),例如,如果用户单击 A 显示B 文本,反之亦然,例如,如果年份是 2020 年向用户 bloc 显示 2020 文本只是一种帮助您实现这一目标的模式(有一些积极的和一些消极的点),并且除了 bloc 之外还有很多模式可以做到这份工作给你

you could study about state and what is state management and why we need it?你可以研究状态,什么是状态管理以及我​​们为什么需要它? in the state management section of the flutter documents link here:在颤振文档的状态管理部分, 链接在这里:

also bloc library official documents are a good place to start learning about the bloc. bloc 库的官方文档也是开始了解 bloc 的好地方。 link here :链接在这里:

BLOC is another state management pattern used by many for flutter and angular. BLOC是另一种状态管理模式,被许多人用于 flutter 和 angular。

In simple words it allows you to update specific widgets when some specific state is declared by the bloc.简而言之,它允许您在 bloc 声明某些特定状态时更新特定小部件。 You can dispatch an event to change the state of the widget which is wrapped with BlocBuilder .您可以调度一个事件来更改由BlocBuilder包装的小部件的状态。

For doing some process other than building widgets, you can use BlockListener and do the process you want based on the state declared by the bloc.对于构建小部件以外的某些过程,您可以使用BlockListener并根据 bloc 声明的state执行您想要的过程。

You can learn more about it here: https://bloclibrary.dev/#/gettingstarted您可以在此处了解更多信息: https : //bloclibrary.dev/#/gettingstarted

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

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