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
5
votes
1 answer

flutter: how to use 'await for' to wait for other BLoC event to finish

on screen init, I am loading my data via an externalData_bloc. On the same screen I am also rendering another widget controlled with internalData_bloc for user input which depends on the number of imported records (how many rows are needed). Of…
w461
  • 2,168
  • 4
  • 14
  • 40
5
votes
2 answers

Same BLoC event not triggering more than one time

I've HomeBloc from HomeView class. Basic UI architecture is as following: HomeView extends StatelessWidget { BlocProvider( child: HomeBody(), bloc: new HomeBloc() ) } HomeBody extends StatefulWidget { createState =>…
shafayat hossain
  • 1,089
  • 1
  • 8
  • 13
5
votes
2 answers

Flutter Bloc equatable state not updating with a Map of Strings property

My Bloc state isn't updating and I've found the problem to potentially be a Map property that isnt being compared properly. Please correct me if I'm wrong, but the state updates when the other properties change, just not…
Dustin Silk
  • 4,320
  • 5
  • 32
  • 48
5
votes
2 answers

MultiBlocProvider not instantiating all bloc providers - how to properly work with MultiBlocProvider?

I am using flutter_bloc and leveraging the MultiBlocProvider widget. Things were working fine, and then at some point, my MultiBlocProvider just refused to instantiate any new Blocs I created and added to it. I am pretty new to flutter_bloc so any…
danoch
  • 171
  • 1
  • 5
5
votes
3 answers

How to provide a BLoC (with flutter_bloc) to `showSearch`

I am using the package flutter_bloc for state management. I want to create a search screen, and found the showSearch Flutter function, and have been having issues providing a BLoC instance to the ListView my SearchDelegate implementation creates. I…
wujek
  • 10,112
  • 12
  • 52
  • 88
5
votes
2 answers

Flutter state not update after yield new state

I am using bloc in flutter app and my problem is view is not update when yield new state. I want to add new message to my list of messages. and I yield same state with new data but my view doesn't update after adding new message to list. Bloc: class…
BeHappy
  • 3,705
  • 5
  • 18
  • 59
5
votes
0 answers

Flutter: Read the Stream data of a BloC state and re-render the UI if it changes

I have a problem with using the BloC pattern in combination with showing a download process using Dio. Can anybody tell me, how to get the onUploadProgress from dio into a bloc state and display it when the progress inside the state updates? At the…
SiLenT_Tz
  • 71
  • 5
5
votes
2 answers

Global dependency on flutter_bloc

I have a problem implementing an app on Flutter using flutter_bloc. I understood the core concepts but I found an "edge case" where there is not examples or guides (at least that I could find): (I will simplify the problem) I have a bloc called…
Ademir Villena Zevallos
  • 1,579
  • 1
  • 13
  • 20
5
votes
1 answer

Missing concrete implementation 'getter Equatable' / issue with props

I am working through many of the tutorials out there on bloc with flutter and running into some inconsistencies. I am using Android studio and creating the bloc code by using the plugin from Intellij v1.6.0. For the bloc_event, I continue to see…
bboursaw73
  • 1,128
  • 2
  • 13
  • 26
5
votes
2 answers

How to listen to state properties changes Flutter bloc pattern

I'm developing a Flutter app and I faced the title's mentioned problem. The BLoC manage the behaviour of a form with properties of a Person Model. So my state class looks like this. class SynchronizedForm extends Equatable { final PersonModel…
5
votes
2 answers

Loading data on init with BLoC

New to Flutter with BLoC. Building off of a search template, looking to have data (items) load on app load instead of on state change. The method getCrystals() returns the correct data when the search intent .isEmpty but how can it be done on app…
JMP
  • 557
  • 1
  • 6
  • 17
5
votes
2 answers

Listen to Events instead of States with Flutter BLOC

I am using the Flutter BLOC library (https://pub.dev/packages/bloc) I know there is a way to "listen to" BLOC states changes (with the listen() function) chatBloc.listen((chatState) async { if (chatState is ChatStateInitialized) { //…
Soms
  • 53
  • 1
  • 3
5
votes
2 answers

Is it ok to pass BuildContext to BLoC?

In Android’s MVVM passing context to viewmodel means breaking the pattern. Should you pass BuildContext to bloc class in Flutter’s Bloc architecture?
March
  • 57
  • 1
  • 5
5
votes
2 answers

what does the child class of Equatable pass to the super(Equatable class)?

hi I am new with bloc in flutter and I'm trying to understand block timer In the doc of flutter_bloc and I would know what's this constructor class mean @immutable abstract class TimerState extends Equatable { final int duration; //and this…
B.Ghost
  • 200
  • 4
  • 15
5
votes
1 answer

Flutter Bloc with Multiple Streams?

I have recently heard and read a lot about the BLoC architecture and it makes a lot of sense and is similar to program structures I have used before. However, one thing I have found unclear is the streams that a BLoC should instantiate and make…
turbo_sheep
  • 109
  • 2
  • 7