简体   繁体   English

Flutter BLoC:Cubits 比 BLoC 更好吗?

[英]Flutter BLoC: Are Cubits better then BLoC?

I'm working with Flutter quite a long time and have a bunch of released products.我在 Flutter 工作了很长时间,并且发布了很多产品。 I never really liked BLoC and preferred to use Provider or later Riverpod.我从来没有真正喜欢过 BLoC,而是更喜欢使用 Provider 或后来的 Riverpod。

I just don't understand that event concept.我只是不明白那个事件的概念。 Why do we still need it?为什么我们还需要它? And i'm confused because of it's actual popularity... BLoC has Cubit subclass that seems to be more simple to use, but everyone just keep telling: "Cubit is simpler, but not so functional".而且我很困惑,因为它的实际受欢迎程度...... BLoC 有 Cubit 子类,似乎更易于使用,但每个人都一直在说:“Cubit 更简单,但不是那么实用”。 But what are limitations?但限制是什么?

I even think that Cubits are MORE USEFUL and MORE SIMPLE at the same time:我什至认为 Cubits 同时更实用和更简单:

  1. With Cubit you just call it's method with params.使用 Cubit,您只需使用参数调用它的方法。 You still can listen its state AND get the method return value if needed too.您仍然可以收听其 state 并在需要时获取方法返回值。
  2. You don't need extra coding implementing these Event Types.您不需要额外的编码来实现这些事件类型。
  3. You don't need extra extra coding implementing how bloc will handle every single event type.您不需要额外的代码来实现 bloc 将如何处理每个事件类型。 METHODS DO THAT JUST FINE.方法做得很好。

example: User taps some product's "Add to cart" button.示例:用户点击某些产品的“添加到购物车”按钮。

Cubit:肘:

cartCubit.addProduct(productId);

BLoC:集团:

cartBloc.addEvent(UserAddsProductEvent(productId));

inside them:在他们里面:

Cubit:肘:

void addProduct(String productId) async {
   //some validation...
   if(...){...}
   final result = await cartRepo.addProduct(id);
   if(result == ...) {
      state = someState;
   //....
}

BloC:集团:

void addEvent(CartEvent event) {
   if (event is UserAddsProductEvent) {
      _addProduct(event.productId)
      } else if (event is....) {
      //.....
      }
}

void _addProduct(String productId) async {
   //some validation...
   if(...){...}
   final result = await cartRepo.addProduct(id);
   if(result == ...) {
      state = someState;
   //....
}

What is the point?重点是什么?

There's a good overview of Cubit vs Bloc in the official documentation .官方文档中对 Cubit vs Bloc 有一个很好的概述。

In short, Cubit's advantage is simplicity , while Bloc provides better traceability and advanced ReactiveX operations .简而言之,Cubit 的优势是简单,而 Bloc 提供了更好的可追溯性先进的 ReactiveX 操作

In our projects, we use both Cubit for simpler cases, and Bloc if the logic is more complicated and some "limitations" actually become useful:在我们的项目中,我们将 Cubit 用于更简单的情况,如果逻辑更复杂并且某些“限制”实际上变得有用,则使用 Bloc:

  • You can emit a new state only as a reaction to an event, so the implementation is more straightforward (but more verbose as well).您可以发出新的 state 仅作为对事件的反应,因此实现更简单(但也更冗长)。
  • All the events are processed one by one.所有的事件都被一一处理。 Again, it makes implementation more reliable and easier to maintain.同样,它使实现更可靠且更易于维护。

Also, that can be a matter of personal preference, but I like Bloc for its close mapping to the FSM pattern.此外,这可能是个人喜好问题,但我喜欢 Bloc,因为它与 FSM 模式的紧密映射。 In most cases, the application state can be nicely represented as a state machine.在大多数情况下,应用程序 state 可以很好地表示为 state 机器。 It's easier to discuss the implementation even with a whiteboard, as you can just show the scheme with a number of states and events changing that state.即使使用白板也更容易讨论实现,因为您可以只显示具有许多状态和事件的方案,这些状态和事件改变了 state。

If you're happy with Cubit, then you probably don't need Bloc.如果您对 Cubit 感到满意,那么您可能不需要 Bloc。 After all, the main goal is to make the architecture easy to understand and to maintain.毕竟,主要目标是使架构易于理解和维护。

BLoC集团

The advantage of having events instead of direct method calls is that you can debounce/throttle, buffer the stream before doing your logic.使用事件而不是直接方法调用的优点是您可以在执行逻辑之前对 stream 进行去抖动/节流、缓冲。

In other words, you can use special methods applicable for events logic.换句话说,您可以使用适用于事件逻辑的特殊方法。

Cubit

If you start with Cubit for a new project, the idea why it exists is that later you will have the ability to migrate from Cubit to BLoC.如果您从 Cubit 开始一个新项目,它存在的想法是稍后您将能够从 Cubit 迁移到 BLoC。

This means that if at the beginning of a project you think that BLoC is too much overhead and you need simpler state management (without events, boilerplate, etc.) you can choose cubit and migrate to BLoC with fewer efforts, than if you selected a different state management solution like MobX or Riverpod.这意味着如果在项目开始时您认为 BLoC 开销太大,并且您需要更简单的 state 管理(没有事件、样板等),您可以选择 cubit 并迁移到 BLoC,而不是选择一个不同的 state 管理解决方案,如 MobX 或 Riverpod。

So with Cubit, you first implement the state and functions.因此,使用 Cubit,您首先要实现 state 和函数。 Later if you decide to switch to BLoC you add events and EventHandler.稍后,如果您决定切换到 BLoC,则添加事件和 EventHandler。

More you can read here (official docs): Cubit vs BLoC您可以在此处阅读更多内容(官方文档): Cubit vs BLoC

Cubits are easy to understand and implement, while still being quite powerful. Cubits 易于理解和实施,同时仍然非常强大。 I wrote a few small to middle sized apps using cubits and hooks, so it's a matter of preference whether one chooses Blocs or Cubits or both.我使用 cubits 和 hooks 编写了一些中小型应用程序,因此选择 Blocs 或 Cubits 或两者都是偏好问题。

You only need to be careful with the things that you emit in state, many times I saw developers (me included;) being surprised that UI doesn't update.你只需要注意你在 state 中发出的东西,很多次我看到开发人员(包括我;)对 UI 没有更新感到惊讶。 It was because the object/list was equal to the previous one, either becase equalTo() wasn't correctly implemented or the list size remained the same.这是因为对象/列表等于前一个,要么是因为 equalTo() 没有正确实现,要么是列表大小保持不变。

I actually wrote an extensive Flutter tutorial showing how to write a clean Flutter app with cubits and hooks.实际上,我写了一篇详尽的Flutter 教程,展示了如何使用 cubits 和 hooks 编写一个干净的 Flutter 应用程序。

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

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