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 instantiate or add an item in a final List with copyWith?

What happens is that I have a class called Drawing with Equatable as follows : class Drawing extends Equatable { final List canvasPaths; const Drawing({ this.canvasPaths = const [], }); @override List get props…
Daniel Roldán
  • 1,328
  • 3
  • 20
0
votes
2 answers

Parent Bloc unreachable after navigation Flutter

I have created the Cubit SurveysCubit in HomePage from BlocProvider component, now, I want to access it from a new page pushed in its child body. All works fine until the pushed page is reached here the following error is shown telling me that the…
Finley Adams
  • 773
  • 1
  • 8
  • 20
0
votes
1 answer

how to hit and get data after getting value from dropdownbutton in flutter

I need to know how to hit API after selecting a value from dropdownButton and how to use that response in text in flutter. Here I can able to hit api and got response but it is using null or value is not changing. I will share image and…
Madhanavel
  • 93
  • 1
  • 11
0
votes
1 answer

How can I test that Flutter BlocListener navigates and renders an initial Welcome page

I am trying to test that a WelcomePage widget is found when my App is first run and no authentication has yet happened. No matter what I try, I cannot verify that the widget gets rendered. The test fails every time. I am using…
Justin Schier
  • 524
  • 5
  • 14
0
votes
1 answer

Unable to display contact names from phone in a list view widget in flutter

I am trying to use bloc to fetch contacts from contact service and display the contact names in an overlay widget in a list view. Can you please check my code and let me know what is wrong as it is not able to fetch any contacts and display the…
nidhi patnaik
  • 19
  • 1
  • 5
0
votes
1 answer

ListView update via BLOC pattern issue

I am learning the Bloc patter and in general my code is working. But my problem is that the BLOC builder doesn't seem to rebuild the ListView properly, as every time it is updated from the BLOC with respect to the ListView data, the position of the…
Konst
  • 61
  • 5
0
votes
0 answers

Flutter How to update list in cubit?

I am using Cubit state managament. I have two page in bottom navigation bar. They are Categories and Words page. I listed all categories inside the categories page, and I updated,deleted and added category in the page. I updated the category, my…
0
votes
2 answers

2 bloc and 1 widget

I have widget (FirstWidget) and 2 providers to this widget on 2 different pages. First screen: BlocProvider( create: (context) => FirstBloc() child: FirstWidget(), ) Second screen: BlocProvider( create: (context) =>…
user18084770
  • 31
  • 1
  • 3
0
votes
1 answer

how to call api while refreshing the screen with bloc in flutter

Stateful: void initState() { _refresh(); super.initState(); } @override Widget build(BuildContext context) { BlocProvider( create:…
Madhanavel
  • 93
  • 1
  • 11
0
votes
1 answer

Flutter Bloc & Cubit

I am following the tutorial here: https://resocoder.com/2020/08/04/flutter-bloc-cubit-tutorial/. When I reached the "Building with Cubit" part and in the main.dart, I have the following issue: The errors I have: [{ "resource": "/d:/My…
TaylorR
  • 3,746
  • 5
  • 25
  • 42
0
votes
1 answer

How to Test Exception in Flutter Bloc Test

I'm trying to write BLoC tests using the bloc_test framework in Flutter. Before anyone tries taking this down, no this isn't the same as testing for exceptions in a widget/unit test for Flutter. BLoC tests are different. I have a cubit that I'm…
0
votes
1 answer

Flutter Bloc Auth wont listen ( stuck at onGenerateRoute )

iam currently learning on bloc , i follow this code https://github.com/felangel/bloc/tree/master/examples/flutter_login but while iam running ,but i got stuck at splashscreen and cant going through the dashboard / loginpage iam using paste bin…
PwS
  • 15
  • 4
0
votes
1 answer

Flutter blocTest Missing type arguments for generic function 'blocTest, State>'

When following the bloc test documentation, I created the following blocTest; blocTest('should do something', build: () => SignInBloc(signIn: mockSignIn), act: (bloc) => bloc.add(const OnEmailChanged(email: 'test'))); However I get the…
c.lamont.dev
  • 693
  • 1
  • 6
  • 14
0
votes
1 answer

Unable to factor out list of `providers` from `MultiBlocProvider`

I want to combine BlocProviders from multiple sources into a single MultiBlocProvider. But any attempt to construct a List of BlocProviders outside of the MultiBlocProvider constructor arguments leads to a runtime error, that the Provider could…
ig-dev
  • 489
  • 1
  • 5
  • 15
0
votes
1 answer

My cubit is not emitting a new state with freezed?

I am trying to use Cubit for state management and I have a state class with @freezed annotation. I've used cubits before and did not see any issues like that before. In cubit constructor I emit initial state with super constructor and BLocBuilder…
shuster
  • 184
  • 3
  • 10
1 2 3
99
100