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
6
votes
2 answers

Flutter bloc migration due to mapEventToState is not working

I am following the migration to the new bloc 8.0.0. I am trying to remove the mapEventToState but I am having a difficulty in doing so. Can you help me how to do it. I have tried it below but it won't work. Old code: class SignInBloc extends…
Mr. Tacio
  • 442
  • 1
  • 7
  • 18
6
votes
1 answer

How to use BlocBuilder to refresh a Dialog in Flutter

I have a dialog and I wrap it in a blocbuilder to update it according to the type of state but the dialog just is built First time and after state has changed it doesn't rebuild. showDialog( context: context, builder: (_) { …
Parisa Baastani
  • 1,713
  • 14
  • 31
6
votes
3 answers

Flutter auto_route | How do I wrap a route with BlocProvider?

So, I'm using the auto_route package for navigation in my app and flutter_bloc for state management. When I was using the old Navigator, I could just wrap a route with a BlocProvider. For example: class Router { static Route
IncorrectMouse
  • 179
  • 1
  • 9
6
votes
2 answers

Flutter: Get Provider if exists otherwise return null instead of exception

When we use BlocProvider.of(context) to access Bloc object, it returns an exception if no OrderBloc exists on ancestor widgets of current context. Returned exceptions as follows: No ancestor could be found starting from the context that…
Siamak S
  • 163
  • 9
6
votes
2 answers

Flutter BLoC `buildWhen` property

I get a _CastError error at the last line of this piece of code BlocBuilder buildUsernameField() { return BlocBuilder( buildWhen: (previous, current) => previous != current && current is EditingUserInfo, …
user54517
  • 2,020
  • 5
  • 30
  • 47
6
votes
3 answers

Using BLoC in a dialog: Could not find the correct Provider

I am attempting to use a bloc to manage the content of a dialog. I am fairly new to flutter. The page is defined as: @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text("Business Region…
Nefarious
  • 458
  • 6
  • 21
6
votes
1 answer

Flutter BloC with nested data structure

I have a nested structure, something like this: List |-- List | |-- List This is the workflow of my app: App is opened Fetch API and display a list of Areas The user chooses an Area and go to next screen Fetch API and…
Ale
  • 2,282
  • 5
  • 38
  • 67
6
votes
2 answers

Emitting states from a bloc using bloc.emit

I'm building a phone authentication ui (OTP) using firebase_auth library with BLoC pattern (a bit of an overkill for this ui, but the whole project is in BLoC, so). I'm processing the authentication within the BLoC, as such: @override …
hman_codes
  • 794
  • 9
  • 24
6
votes
1 answer

Where and when should I store values in BLoC itself or its state

Context I come from Redux and I am learning the BLoC pattern for global state management. I am having troubles defining where should I store values as properties inside the BLoC class, and when I should store values inside States. Use case I have a…
Raul Mabe
  • 453
  • 5
  • 15
6
votes
3 answers

I have this problem when I try to do flutter pub get. How to solve this problem?

I created a new flutter project where I put the flutter_bloc dependency but when I do flutter pub get I have this error. [bloc] flutter pub get Running "flutter pub get" in bloc... Because bloc depends on flutter_bloc ^6.0.1 which depends on bloc…
biros
  • 321
  • 2
  • 7
6
votes
1 answer

How to keep the state using hydated_bloc in flutter?

I'm trying keep the state using the library hydrated_bloc, the problem is what all example of the this library are very basic and i want to maintain the state by consuming an example api rest but I still can't implement the logic of this…
Steven Colocho
  • 381
  • 1
  • 5
  • 15
6
votes
2 answers

Design recommendations for using flutter bloc library and websockets together

We have a Flutter application that uses websockets for server initiated communication. We use flutter_bloc as the state management mechanism across the app. UI events are conveyed to the widget through Bloc state transitions and BlocBuilder…
samantp
  • 499
  • 1
  • 5
  • 11
6
votes
1 answer

How to make widget test wait until Bloc has updated the state?

I have a EmailScreen (stateful widget) that has a text input, and a button. The button is only enabled when a valid email is input. I'm using Bloc, and my screen has InitialEmailState and ValidEmailInputState, and it works fine when I run the…
Hilton Pintor
  • 300
  • 2
  • 8
6
votes
1 answer

Effects of Injecting BLoC as a singleton using DI and where to close stream?

I am using inject.dart to inject my bloc as a singleton, is it a bad practice i.e that causes memory leak? Since all the streams I am using are Broadcast Streams from RxDart I was wondering if a singleton would work better? And if not a bad…
Ajay Rn
  • 93
  • 7
6
votes
3 answers

BLoC: how to pass it?

I would like to know the best way to pass the bloc. I read about the bloc providers, but what`s the difference between using them and just passing the bloc in the constructor like: ExampleView X = ExampleView(bloc,...) Actually I find this way…
Little Monkey
  • 5,395
  • 14
  • 45
  • 83