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

Can n't pop the current screen getting '!_debugLocked': is not true

I'm using bloc state management for update state. I want to pop current screen to listen state changes from bloc listener but shows '!_debugLocked': is not true on console. There are some similar question I try there solutions like: 1st…
Yousuf Ali
  • 159
  • 2
  • 5
0
votes
1 answer

flutter bloc implementations deviations

I am trying to implement a similar app to counter app called japa counter , it has additional functionality to decrement counter . as well it maintains number of rounds and beeds . where when counter(beeds count)goes to 108 , rounds count get…
0
votes
1 answer

Migrating to bloc 8.0.1 from 3.x, getting depreciated errors

I have updated from bloc 3.x version to 8.0.1 but I am fairly new to bloc. I have read down the basics and edited some of it but I am stuck now. This is my events, bloc and state file. How can I convert it to code compatible with bloc…
aneebhiba614
  • 33
  • 1
  • 5
0
votes
1 answer

Access state from ui without an if statement

am following this Bloc's official example and I couldn't find a way how to access the state without that if statement. Let's have the example below, I would like to display a specific text based on the initial value of showText, the only possible…
A Akrm
  • 97
  • 7
0
votes
1 answer

In flutter_bloc, why does the official documentation suggest modifying the state by copying it and emitting a new instance?

I am working on a Flutter app and using the cubit system in flutter_bloc for state management. This is my first time using bloc, and I am using the Tutorials on bloclibrary.dev as a guideline. (ex:…
pmichel
  • 3
  • 3
0
votes
1 answer

Bloc Provider error when pop back just for 1 second

I have a profile page where I can click on the image to navigate to the edit page. And all are working perfectly working fine with bloc state management. But when pop back to the profile page there is a bloc provider not found error just for some…
Aswanath C K
  • 171
  • 11
0
votes
1 answer

flutter bloc:How to provide a single instance of a cubit for a bottom bar screen and a normal Screen?

I have a navigation bottom bar in my app. I created a custom class for routing that sets up all the logic for navigating to all the screens in the app, but the bottom bar pages are not in this custom class. I have a problem providing a single…
Mary
  • 51
  • 3
0
votes
1 answer

How can I access/manipulate data from a Stream inside a Bloc in Flutter?

After calling a method in my Bloc that returns a Stream from Firebase, I need to perform some logic on the data that comes from the stream (e.g. filter the data based on the userId). The problem I run into is that I can't use async/await on…
user3735816
  • 225
  • 2
  • 11
0
votes
1 answer

Target of URI doesn't exist: 'package:flutter_bloc/flutter_bloc.dart'. Android Studio

I created a brand new flutter project, I added bloc: ^8.0.3 and equatable: ^2.0.3 to pubspec.yaml file, I just imported import 'package:flutter_bloc/flutter_bloc.dart'; in the main file, and it gives me the above error Target of URI doesn't exist:…
A. Albrg
  • 485
  • 4
  • 21
0
votes
1 answer

While using Bloc Listener I am getting an error ProviderNotFoundException (Error: Could not find the correct Provider above

I am learning bloc and was making a todo app but while calling my bloc listener in addition to do screen it is throwing an error. Error is showing in Add To-Do Class The exception has occurred. ProviderNotFoundException (Error: Could not find the…
Pratul Pant
  • 334
  • 3
  • 12
0
votes
1 answer

Using two Repositories in one Cubit - Flutter BLoC

I was wondering if I could instantiate two Repositories in one Cubit ? Like so : class LoginCubit extends Cubit { LoginCubit(this._authRepository, this._authenticationRepository) : super(const LoginState()); final…
Sunshine
  • 372
  • 2
  • 21
0
votes
1 answer

The following assertion was thrown running a test: pumpAndSettle timed out

here is a test case which test if i failded to add data to my database it will show error message in snackbar but my test showing pumpAndSettle timed out error testWidgets( 'renders error snack bar ' 'when status changes to failure',…
Mamun Hossain
  • 21
  • 1
  • 5
0
votes
1 answer

error migrating to updated flutter_bloc & testing with mockito

I'm trying to run a test with mockito following an outdated tutorial for a messaging app in flutter. I'm trying to use the most updated versions of everything. The tutorial just implemented flutter bloc, and now I'm getting an error that I don't…
JohnBradens
  • 119
  • 8
0
votes
2 answers

BlocProvider.of() called with a context that does not contain a RegisterCubit

the error show up when I try to create a new user, registr page worked yesterday and I have not changed anything on it. Can you help me? Here is my code register cubit class RegisterCubit extends Cubit { RegisterCubit() :…
Mus
  • 67
  • 1
  • 13
0
votes
1 answer

Flutter Bloc pattern: nested api calls in event handler

I am relatively new to Flutter and still trying to get familiar with the Flutter Bloc pattern. I have created an AuthBloc to handle user authentication. The auth flow is as follows : #1 - user signs in using credentials (first endpoint is…