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

How to access bloc state properties that don't exist in the base state class?

Consider these states: class ExerciseInitialState extends ExerciseState {} class ExerciseLoadingState extends ExerciseState {} class ExerciseLoadedState extends ExerciseState { final List data; const ExerciseLoadedState({required…
kozhioyrin
  • 400
  • 4
  • 15
0
votes
0 answers

Why I'm getting empty actual array from blocTest function?

So I'm implementing blocTesting for my project and I'm getting an empty actual array from a specific feature. share_bloc_test.dart Below is the code for the blocFunction that is returning the empty actual array. group('ShareDisconnectGrantor',…
Nik
  • 187
  • 3
  • 17
0
votes
1 answer

Flutter Bloc Stream Listen not listening to state change

I have two blocs ( WorkOrderBloc and FilterBottomSheetBloc ) that need to communicate with each other. MultiBloc Provider I configured a MultiBloc Provider and I am passing the WorkOrderBloc to FilterSheetBottomBloc through its…
0
votes
1 answer

Flutter Bloc-why i am getting error state as output while using bloc ,

flutter bloc state management issue i am learning bloc with openweatherapi,but iam getting Errorstate(Weathererrorstate) as output, not understanding with bloc state management, did'nt added in code i wrapped main app with blocprovider not…
Diljith
  • 69
  • 8
0
votes
1 answer

Combining bloc and websockets

I'm trying to use bloc with websockets in both directions (i.e. when a message is received on the websocket, an event is fired as well as when a state is emitted, a message is sent over the web socket). I'm still fairly new to flutter but have…
Darc
  • 745
  • 10
  • 26
0
votes
0 answers

how to stretch context between 2 widgets in flutter?

I have two different widget. The second widget I use in the middle of the other. The problem is that I don't understand how to extend the context to the next widget. fіrst widget: Widget _getRow({ required String name, required String…
Andrii Havrylyak
  • 675
  • 6
  • 16
0
votes
2 answers

How to create a simple POST request via bloc?

I need to create a simple POST request via bloc. To send it when the button is clicked. How can i do this? I need a simple request without parsing. Just send. My button: OutlinedButton( onPressed: _sendReq, child: Text("Send…
userName
  • 903
  • 2
  • 20
0
votes
1 answer

How to emit empty state more than one time when I am using equatable

I am trying to emit a state more than one time because I am validating a form no need to add any thing on the constructor and make copyWith func etc.. so could you help me with other solutions? class PersonUnValid extends PersonState { const…
Mo_
  • 45
  • 9
0
votes
4 answers

Flutter: Try to use BlocProvider with get_it and auto_route package but context don't find provider

I try to make a login page for my app who permit to access when password and username are okay. I have some problem with my bloc provider. My application uses the auto_route package for routing, get_it for dependency injection and block to handle…
TheSpyer56
  • 1
  • 1
  • 2
0
votes
1 answer

How to change text after button click using Bloc flutter

So I'm learning basics of bloc and I wanted to know how to change a text using a button without setstate(), this whole time ive been using setstate but I would like to know how to change a text after a button click using bloc, state, event. This is…
LaurenTsai
  • 31
  • 9
0
votes
1 answer

Two flutter cubits on one screen

I have a screen (a stateless widget) in which I have two buttons, one to check if Firebase account is verified and the second for resending verification email. These two processes are obviously separate and I need to separate them in two cubits. So,…
Draško
  • 2,119
  • 4
  • 41
  • 73
0
votes
2 answers

Why is the BloC model information updated in this case?

Creating a page to edit a user's profile I initialize a variable with the user's information stored in a model and create a user with that information to edit it: late UserModel user; late UserModel userUpdate; @override void initState() { user…
ivangl
  • 35
  • 4
0
votes
1 answer

For Which API call I should use bloc?

I'm new to flutter, I have some questions about bloc and I'm working on small projects to develop skills. Could you help me to know about bloc and improve? I used bloc on every screen (every API call - GET,POST) From the backend, I'm only getting…
Madhanavel
  • 93
  • 1
  • 11
0
votes
1 answer

Flutter - make api call inside Listview

I am calling an api, which will return a list of items (workouts), and items will be shown in a ListView. but I also need to call another api for each item of the ListView to fetch the score of that workout (if there is already a score) and show the…
Jean Cuervo
  • 55
  • 1
  • 4
0
votes
0 answers

Flutter BlocConsumer doesn't listen to state change when searching

I have been battling with this flutter bloc problem. I am currently using flutter Bloc 7.0.1. The BlocConsumer doesn't listen to the state changes at all. Anytime I enter values inside the search field, event is been called and state is yielded but…