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
8
votes
3 answers

How should logic in a BLoC trigger a one-time UI alert, like a dialog or snackbar?

When using the BLoC pattern to isolate business logic from the view (without the bloc library), how can we trigger a one-time UI call (like showing a snackbar or an alert dialog) when some asynchronous logic has finished in a BLoC? For example, if a…
Bryson Thill
  • 494
  • 4
  • 13
8
votes
2 answers

How to use bloc pattern between two screens

my main.dart file looks like this. home: MultiBlocProvider( providers: [ BlocProvider( create: (BuildContext context) => UserBloc()..add(GetUser(userId:"5e0b62023b75dd60e22edaad")), ), …
Udith Shalinda
  • 487
  • 1
  • 8
  • 20
8
votes
3 answers

How to set Firebase Analytics custom events in Flutter without passing 'analytics/observer' object in each screen

I'm setting up Firebase Analytics Package for my Flutter project. The sample provided in the library passes the analytics object for tracking events and observer for tracking the tab changes. class MyApp extends StatelessWidget { ... Widget…
Mayur Dhurpate
  • 1,112
  • 4
  • 16
  • 34
7
votes
0 answers

What's the difference: BlocBuilder & buildWhen vs BlocSelector

I'm looking at the comment docs /// An optional [buildWhen] can be implemented for more granular control over /// how often [BlocBuilder] rebuilds. /// [buildWhen] should only be used for performance optimizations as it /// provides no security…
TpoM6oH
  • 8,385
  • 3
  • 40
  • 72
7
votes
2 answers

Listening of bloc getting called multiple times

I have this code for listening to bloc on my screen. late MyBloc myBloc; @override void initState() { print('inside init state'); super.initState(); myBloc = BlocProvider.of(context); myBloc.stream.listen((state) { …
inter cretasyo
  • 149
  • 1
  • 12
7
votes
3 answers

Bloc observer not showing log

Hey I've been bloc observer as the main state management tool in my flutter app and using it made things much easier. The bloc observer is the main tool I use to debug and observe things happening. But after migrating to the Bloc v8.0.0 bloc…
biniyam112
  • 956
  • 1
  • 11
  • 17
7
votes
1 answer

How should I manage the state of a large model in Flutter?

I'm writing my first Flutter app and struggling with the variety of state management solutions. I decided to start with Provider, but I'm thinking about switching over to BLoC. So far most of the examples I've found are limited to relatively…
Mark R
  • 341
  • 3
  • 10
7
votes
2 answers

Flutter, is rx dart required for BLoC?

In the bloclibrary official site documentations I have seen, I have never seen they used rx dart. However, sometimes in the community, they saying it is much better to use rx dart together. But I don't understand. I'm using firestore as the backend…
dontknowhy
  • 2,480
  • 2
  • 23
  • 67
7
votes
1 answer

How to test a sealed state class in Flutter?

I'm using Freezed to generate sealed data classes in my Flutter app. I need to test it and I don't know how to do so. Any idea? This is the state: @freezed abstract class LoginState with _$LoginState { const factory LoginState({ @required…
Luis Utrera
  • 942
  • 6
  • 15
7
votes
2 answers

Dispatch first bloc event or cubit method, on page start inside StatelessWidget

I have 10 buttons in main menu of my app and each of them contains BlocBuilder inside them. So when I click on those buttons to open a new page, I want to dispatch the first event, but I don't know how. I can change all classes to stateful widget…
SardorbekR
  • 1,388
  • 3
  • 18
  • 33
7
votes
1 answer

How to call bloc event only once when the screen loads

I'm using flutter_bloc library. My screen widget code looks looks this: class RegisterScreen extends StatefulWidget { RegisterScreen(); @override _RegisterScreenState createState() => _RegisterScreenState(); } class _RegisterScreenState…
Abozanona
  • 2,261
  • 1
  • 24
  • 60
7
votes
2 answers

Modal Bottom Sheet and Bloc

I get the following error when I run code similar to the below code: BlocProvider.of() called with a context that does not contain a Bloc. To replicate BlocProvider( create: (context) => getIt() child:…
Gerry
  • 1,159
  • 1
  • 14
  • 29
7
votes
1 answer

How to change state of individual list items using bloc flutter?

How to change the widgets in a list item in flutter using bloc pacakage. Should i use BlockBuilder or listener on the whole ListView.builder or only the individual items. It would be nice if u share an example or tutorial. eg If i have a checkbox i…
Harshvardhan R
  • 407
  • 2
  • 7
  • 12
7
votes
3 answers

Bloc widgets reftech data (rebuild) when navigating to another pageView or any View altogether

Context: I have an App that has a PageView to navigate between five screens using BottomNavigationBar, One page is making use of the Bloc Pattern (almost all of the pages will use Bloc in future releases) these Blocs are fetching data from backend…
Waleed Alrashed
  • 777
  • 1
  • 7
  • 20
7
votes
2 answers

Flutter BLoC mapEventToState gets called only the first time for an event and not called each next time that event is fired

I have Courses and Tasks. Each Course has many Tasks. That is why I am using different screens in the app to show a list of courses and after a tap on a course, I am navigating to the next screen - a list of tasks. Here is my onTap method of the…