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
5
votes
0 answers

Stream per data or Stream per ViewModel in Flutter - Which is recommended?

I'm using the BLoC pattern for state management in my Flutter app and I've used these two methods to manage state data.e 1. making each piece of data have its own stream and using StreamBuilder for each widget that needs the data in the UI …
ibnhamza
  • 861
  • 1
  • 15
  • 29
5
votes
1 answer

Dart/Flutter - yield from callback function

I need to make a function call which does not return anything (void). Only way to get notified about function completion is sending a callback function. Now I an using BLoC pattern along with ReDux, When an event is dispatched, I dispatch another…
Mehul Prajapati
  • 1,210
  • 2
  • 11
  • 29
5
votes
1 answer

Can BlocBuilder of flutter_bloc avoid rebuild part of Widget which not changing?

BlocBuilder of flutter_bloc is kind of put all state of page together. There's a case for pulling a list there's 2 data (isPullingState, dataList), how can I avoid build widget part of dataList when dataList not change, but build widget part of…
JerryZhou
  • 4,566
  • 3
  • 37
  • 60
5
votes
2 answers

How to maintain Flutter Global BloC state using Provider on Hot Reload?

I seem to lose application state whenever I perform a hot reload. I am using a BloC provider to store application state. This is passed at the App level in the main.dart and consumed on a child page. On the initial load of the view, the value is…
KennyP
  • 53
  • 1
  • 5
5
votes
1 answer

Flutter_bloc get updated data from firestore without UI event

I am using flutter to exchange firestore data from few devices. If I use StreamBuilder everything works fine, but I do not like mixing business logic with UI. I would prefer using BLOC as pattern using flutter_bloc plugin. But flutter_bloc works in…
J. Pablo García
  • 499
  • 7
  • 19
5
votes
3 answers

Flutter error while triying to implement BLoC from youtube-search-tutorial

I'm having trouble implementing BLoC in flutter, i followed this tutorial: https://github.com/ResoCoder/youtube-search-flutter-bloc But i get the following compiler message that i haven't been able to debug: Compiler…
Rafa He So
  • 473
  • 3
  • 12
5
votes
2 answers

Flutter BLoC: Is using nested StreamBuilders a bad practice?

Is there a better way to expose a widget to two or more streams from different BLoCs? So far I have been using nested StreamBuilder's for as many streams as I need listening to like the pasted code below. Is this a good practice? StreamBuilder( …
Ali Amin
  • 355
  • 1
  • 2
  • 8
5
votes
1 answer

Problem using RefreshIndicator widget with bloc pattern

I have a class called A which is a Stateless class and I have a class called B which is a Stateful class The build method of A class is as follows @override Widget build(BuildContext context) { return BlocProvider( bloc:…
user10241787
4
votes
1 answer

Initiate global bloc on Logout

I'm providing bloc at top of all view to access at globally. When I'm doing logout and re-login without closing app, event not called because it is already initiate. Here, I want to refresh bloc on logout, so that FetchList and UpdateCount events…
4
votes
1 answer

Flutter Bloc Todo example - Listen to a single todo from the repository

I am learning Flutter and the BLoC pattern from the Bloc library. I think I just about understand the Todo example, but I'm trying to build upon it to do something just a tad more complex, and I'm hitting a wall. So, my issue is this. Let's consider…
Gohu
  • 2,050
  • 2
  • 18
  • 17
4
votes
1 answer

Flutter access BloC without Context

I have a problem with my BloC provider in one of my Flutter applications. I use Chopper as HTTP client and it creates a Chopper instance right at the beginning of the app which I then inject with get_it wherever I want to fetch data from my API. In…
morzan1001
  • 73
  • 4
4
votes
1 answer

Flutter: Many bloc providers cluttering widget tree

Im making a type of chat app, and has a need for multiple blocs/repositories/etc. This has lead to my widget tree looking like this: And also has lead to a nasty looking build method in my main.dart: @override Widget build(BuildContext context)…
4
votes
0 answers

Using flutter bloc emit.forEach with multiple streams

I am trying to use flutter_bloc with multiple streams from my location service and I need to accommodate multiple streams from the service to either emit a state or further get the data from other streams. Future _onGetLocation(GetLocation…
Abuzar Rasool
  • 79
  • 1
  • 9
4
votes
2 answers

(EXCEPTION CAUGHT BY GOROUTER) Failed assertion: line 299 pos 13: '!redirects.contains(redir)': redirect loop detected:

I am trying to use GoRoute, Stream, and Bloc. So, I am implementing Auth Process. So, if the user is not log-in they can't access any page. And, `Stream allows to constantly listen to AuthStatus (Bloc State). But, so how I am not sure, why but I am…
Harshit
  • 95
  • 2
  • 8
4
votes
2 answers

Flutter BLoC : how to update state with null values?

How do you deal with "accepted null values" when you update a state in BLoC ? I use the flutter_bloc package. I have a form in which numeric variables are nullable so that I can check their validity before the form is submitted. But when I emit a…
Galactose
  • 175
  • 3
  • 8