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
17
votes
4 answers

How to resolve emit was called after an event handler completed normally bloc error?

I am using flutter bloc to make download progress percentage displayed but I keep getting this problem. I figured the problem arises in the onDone method but I couldn't figure out how to fix it. ERROR : Exception has occurred. _AssertionError…
biniyam112
  • 956
  • 1
  • 11
  • 17
17
votes
2 answers

BlocBuilder not updating after cubit emit

UPDATE After finding the onChange override method it appears that the updated state is not being emitted #confused UPDATE 2 Further debugging revealed that the StreamController appears to be closed when the updated state attempts to emit. For some…
RemeJuan
  • 823
  • 9
  • 25
16
votes
0 answers

BLoC pattern best practice - is it legit to store data in the BLoC class?

I am new to Flutter and the BLoC design pattern. However, the concept of BLoC looks pretty straightforward and I think I understand it pretty well. Occasionally, when reviewing my coworkers' code, I see that when implementing a BLoC, they tend to…
Avi
  • 826
  • 6
  • 16
15
votes
4 answers

Flutter BLoC library: Where to keep the TextEditingController object: in State, in BLoC / Cubit class or in the widget?

While using BLoC library we store all the variables in a state class. But where to store TextEditingController, which does not change, but the value of it does? Let's say I have a state class like this (Just as example): @freezed abstract class…
Thomas
  • 851
  • 9
  • 15
15
votes
1 answer

Attempting to hot reload Flutter app restarts whole app

I'm working on a Flutter app and I've noticed that whenever I save my app to see the updated changes, my entire app reloads. I would expect the page that I have open to hot reload with my new changes on it. I think that the way my widget tree is…
Lewis Cianci
  • 926
  • 1
  • 13
  • 38
15
votes
1 answer

Flutter BLoC - How to pass parameter to event?

Trying to learn BLoCs I came up with this problem. I have some code in which I generate some buttons with BLoC pattern. However, I have no clue how to update specific buttons properties with dispatch(event) method. How to pass parameters to the…
prkmk
  • 343
  • 1
  • 4
  • 11
15
votes
2 answers

BlocProvider.of() called with a context that does not contain a Bloc of type CLASS

in flutter i just learn how can i use Bloc on applications and i want to try to implementing simple login with this feature. after implementing some class of bloc to using that on view i get error when i try to use this code…
DolDurma
  • 15,753
  • 51
  • 198
  • 377
14
votes
4 answers

How to Refresh the UI in ListView.Builder using flutter GetX when data is changed?

I'm refactoring my app to GetX state management for less boilerplate code. I make the Controller and the API provider (code below). But when I want to refresh the data (Manually too) it won't change. home_page.dart class HomeUI extends…
J.Maher
  • 312
  • 1
  • 4
  • 14
14
votes
2 answers

Flutter Bloc state change is not updated UI with get_it

I've been building a login/authentication feature using a combination of this login tutorial and the resocoder clean architecture tutorials. It's 99% working perfectly, but it is not responding properly to the LoginButton being pressed. For some…
Gary Frewin
  • 505
  • 3
  • 16
14
votes
3 answers

flutter_bloc : Make initialState method async

I am using the flutter_bloc package to manage state in my app. I have a usecase where I have to load the initial state from the remote DB. This requires the initialState method to be async, which it is not. If not by using the initialState method,…
sharath
  • 3,501
  • 9
  • 47
  • 72
14
votes
3 answers

how to access flutter bloc in the initState method?

In the code shown below , the dispatch event is called from within the build method after getting the BuildContext object. What if I wish to do is to dispatch an event during processing at the start of the page within the initState method itself ?…
Natesh bhat
  • 12,274
  • 10
  • 84
  • 125
14
votes
1 answer

Flutter BLoC multiple BLoCs same widget

I'm relatively new to Flutter and the BLoC pattern, so I'm still trying to wrap my head around everything. Let's say I have a quiz app where I have a BLoC called QuestionBloc which uses a repository to fetch questions from a file. Event on…
hoan
  • 1,277
  • 3
  • 18
  • 32
14
votes
3 answers

Flutter BLoC: Navigator.pop in StreamBuilder in build() method

I'm following BLoC pattern and subscribing to stream, and reacting to state changes in build method. When data is loaded I want to close the screen. @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( …
Vadims Savjolovs
  • 2,618
  • 1
  • 26
  • 51
13
votes
2 answers

How can Bloc listen to stream and emit state

In my flutter app, I use flutter_bloc for state management. The bloc in question uses a repository. The repository subscribes to a websocket, and new data is added to a stream. Problem: My bloc listens to the stream: InvestmentClubBloc({ …
Vingtoft
  • 13,368
  • 23
  • 86
  • 135
13
votes
2 answers

The method 'bloc' isn't defined for the type 'BuildContext'

Widget build(BuildContext context) { final appBar = AppBar( title: Text( "Romantic Comedy", ), actions: [ IconButton( icon: Icon(Icons.search), onPressed: () { showSearch( …
Abraham Mathew
  • 2,029
  • 3
  • 21
  • 42