Questions tagged [bloc]

BLoC stands for Business Logic Component. The application implementation pattern of using BLoC is called BLoC pattern.

BLoC stands for Business Logic Component. The application implementation pattern of using BLoC is called BLoC pattern. It was conceived by Cong Hui, Paolo Soares, and Wenfan Huang at Google.

BLoC is a way to centralize your business logic in a single class of your app using Streams, reducing the maintenance issues that could arise with multiple code bases. It was created with the intent of allowing you to share code between Flutter (mobile) and AngularDart (web) applications, but it can be used for clarity without necessarily sharing code.

The BLoC pattern was presented at DartConf 2018 with livecoding by Paolo and can be seen on YouTube.

Some notes about the pattern:

  • BLoC doesn’t assume a particular way to get access to the component. It might be with an InheritedWidget, by passing it down manually through constructors, or using some form of automatic dependency injection.

  • You should avoid having one BLoC as a parameter of another BLoC. Instead, plug only the required outputs to the applicable inputs.

  • Large apps need more than one BLoC. A good pattern is to use one top-level component for each screen, and one for each complex-enough widget. Too many BLoCs can become cumbersome, though. Also, if you have hundreds upon hundreds of observables (streams) in your app, that has a negative impact on performance. In other words: don’t over-engineer your app.

  • In a hierarchy of BLoCs, the top-level (screen) BLoC is normally responsible for plugging streams of children BLoCs to each other.

  • BLoC is compatible with server logic. The pattern doesn’t force you to reimplement that logic on the client (like Flux/Redux would). Just wrap the server-side logic with a component.

  • One disadvantage that stems from the asynchronicity of streams is that when you build a StreamBuilder, it always shows initialData first (because it is available synchronously) and only then does it show the first event coming from the Stream. This can lead to a flash (one frame) of missing data. There are ways to prevent this — stay tuned for a more detailed article. p.s. If using rxdart version 0.19.0 and above, you can just use ValueObservable for outputs and the flash of async is no longer an issue.

  • The inside of the BLoC is often implemented in a purely functional-reactive way (no auxiliary state, pure transformations of one stream to another). But don’t feel obligated to do it this way. Sometimes, it’s easier and more readable/maintainable to express the business logic through hybrid imperative-functional approach.

References:

2009 questions
72
votes
6 answers

Bloc, Flutter and Navigation

So like most, i'm new to Bloc and flutter and dart and wrapping my head around. I've googled, looked through the posts here but haven't found really any answers. So this is about navigation with bloc and flutter. Take the example of a Login. So…
paulinventome
  • 1,047
  • 2
  • 10
  • 11
58
votes
4 answers

What is the difference between Cubit and Bloc?

I am a bit confused about the new release of Bloc: 6.0.0, adding Cubit notion, is the bloc depreciated or we can use both of them?
Ayoub Boumzebra
  • 3,885
  • 3
  • 21
  • 37
49
votes
10 answers

flutter: Unhandled Exception: Bad state: Cannot add new events after calling close

I am trying to use the bloc pattern to manage data from an API and show them in my widget. I am able to fetch data from API and process it and show it, but I am using a bottom navigation bar and when I change tab and go to my previous tab, it…
geekymano
  • 1,420
  • 5
  • 22
  • 53
41
votes
6 answers

BlocProvider.of() called with a context that does not contain a Bloc - even that it does

First of, I do know how BLoC suppose to work, the idea behind it and I know the difference between BlocProvider() and BlocProvider.value() constructors. For simplicity, my application has 3 pages with a widget tree like this: App() => LoginPage() =>…
Stahp
  • 972
  • 1
  • 9
  • 16
36
votes
3 answers

MVVM vs Bloc patterns

I'm creating a new app with Flutter, and I'm trying to design it, separating the business logic from the view. I've read about Bloc and MVVM (I know there are other patterns but these were the ones I preferred), but I don't understand the…
niegus
  • 1,698
  • 4
  • 22
  • 34
32
votes
3 answers

Triggering initial event in BLoC

example_states: abstract class ExampleState extends Equatable { const ExampleState(); } class LoadingState extends ExampleState { // } class LoadedState extends ExampleState { // } class FailedState extends ExampleState { …
vendrick
  • 478
  • 1
  • 5
  • 12
32
votes
3 answers

What is copyWith and how can I use it in Flutter and what are it's use cases?

//File: email_sign_in_model.dart class EmailSignInModel { EmailSignInModel({ this.email='', this.formType=EmailSignInFormType.signIn, this.isLoading=false, this.password='', this.submitted=false, }); final String email; …
Milan Poudel
  • 602
  • 1
  • 9
  • 20
32
votes
10 answers

How to access Provided (Provider.of()) value inside showModalBottomSheet?

I have a FloatingActionButton inside a widget tree which has a BlocProvider from flutter_bloc. Something like this: BlocProvider( builder: (context) { SomeBloc someBloc = SomeBloc(); someBloc.dispatch(SomeEvent()); return someBloc; …
Henrique Arthur
  • 988
  • 1
  • 9
  • 17
27
votes
1 answer

How to add animated transitions when changing Widget on BLoC pattern?

so I was following bloc login tutorial, and while I managed to complete it, I'm still fairly new to Flutter & Dart. There is a portion of the code where, depending on the state, the code returns a different widget, instead of a new Scaffold. Since…
herrmartell
  • 3,057
  • 4
  • 18
  • 27
22
votes
7 answers

Error: The method 'ancestorStateOfType' isn't defined for the class 'BuildContext'

When using the class Bloc provider as follows, I get the error: 'The method 'ancestorInheritedElementForWidgetOfExactType' isn't defined for the type 'BuildContext'.' So I replaced this line…
midi
  • 3,128
  • 5
  • 30
  • 47
21
votes
6 answers

Bloc: is it possible to yield 2 time the same state?

In the login view, if the user taps on the login button without having inserted his credentials, the LoginFailState is yield and the view reacts to it. If he taps again, this LoginFailstate is yield again, but the view doesn't react to it. So, is…
Little Monkey
  • 5,395
  • 14
  • 45
  • 83
21
votes
3 answers

Choosing the correct Flutter design pattern

I have created a Flutter page which has a bunch of inputs in it. I thought this is a mess, let's refactor it and created a new stateful widget for each input. This is great except the data needs to be in the parent widget and I am having a hard…
ebg11
  • 906
  • 1
  • 12
  • 33
19
votes
11 answers

A global key was used multiple times inside one widgets child list

After closing the app, when I try to open it again, I'm getting the following error but it's only on iOS platform, Android works well. I have looked around and there are several SO questions and issues about this problem but I couldn't solve it.…
cipli onat
  • 1,943
  • 1
  • 11
  • 26
19
votes
1 answer

Scoped Model, BLoC pattern, StreamBuilder and Inherited Widget(or Model) which one should I chose and why?

I think I have now at least a vague idea of how to use a BLoC, Stream Builder and Inherited Widget(or Model) in my app (nothing special, but for me it took time), but playing with the Scoped Model I had a sort of existential crisis: I feel they can…
18
votes
2 answers

Flutter State Management (BloC): Stateless vs Stateful widget

So I am reading through Bloc for state management for flutter. Since Bloc allows you to sink and stream (rebuilding a widget based on the input), then is it possible to build an app mostly with stateless widgets? As an example, let say I make lots…
Steve Kim
  • 5,293
  • 16
  • 54
  • 99
1
2 3
99 100