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

How to properly set value of DropdownButton using Bloc in Flutter?

I'm new in Bloc programming pattern and I'm having an issue when using it in with Dropdown That's in my bloc class: final _dropDown = BehaviorSubject(); Stream get dropDownStream => _dropDown.stream; Sink get dropDownSink =>…
GustavoAndrade
  • 247
  • 3
  • 15
9
votes
1 answer

runZonedGuarded in flutter

// Copyright (c) 2021, Very Good Ventures // https://verygood.ventures // // Use of this source code is governed by an MIT-style // license that can be found in the LICENSE file or at // https://opensource.org/licenses/MIT. import…
MUHAMMAD SHAHID RAFI C P
  • 1,067
  • 1
  • 12
  • 25
9
votes
1 answer

Flutter BLOC and Provider how to register them together

I'm writing an app using BLOC architecture and registered bloc providers like this in the main.dart: runApp(MultiBlocProvider(providers: [ BlocProvider( create: (context) { return OrderBloc()..add(OrderInitialEvent()); …
Thanh Pham
  • 203
  • 2
  • 13
9
votes
2 answers

Flutter : Can't get array json using dio to list of object

I tried to load array json (array with no key) and fetch into list of object, here is my sample code : My ApiCliet : Future> fetchOnboarding() async { try { Response response = await dio.get("api/OnboardingItem"); …
Bali Codes
  • 581
  • 1
  • 5
  • 8
9
votes
1 answer

Multi bloc builder

here is the situation - my flutter app dashboard screen has two separate list pageView on top and List item i have a bloc named dashboard_bloc and dashboard_event with two separate event to get pageView data and list item data respectively and…
avinash
  • 501
  • 2
  • 8
  • 16
9
votes
1 answer

how to use same bloc in multiple widgets

I'm facing issue in my current project so created this question im unable to get same bloc state in other widget i tried this https://github.com/felangel/bloc/issues/74#issuecomment-457968962 but i'm getting BlocProvider.of() called with a…
arjun more
  • 189
  • 4
  • 14
9
votes
5 answers

TextField focus stuck when dropdown button is selected

I have a text field and a drop-down menu, both controlled by a Bloc. The problem I have is that as soon as the text field gets selected, it won't give up the focus if the user then tries to select something from the dropdown menu. The menu appears…
Kris
  • 3,091
  • 20
  • 28
9
votes
1 answer

Flutter : Is provider an alternative to the BLoC pattern?

I know that BLoC in flutter acts like the viewmodel layer in android's MVVM, so the data does not gets fetched again and again upon configuration changes (for ex: change in screen orientation). I am confused if provider replaces the functionality…
curiousgeek
  • 903
  • 11
  • 18
8
votes
3 answers

Info: 'runZoned' is deprecated and shouldn't be used. This will be removed in v9.0.0. Use Bloc.Bloc.transformer instead

I am getting this issue while trying to run my code on DartPad. 'runZoned' is deprecated and shouldn't be used. This will be removed in v9.0.0. Use Bloc.Bloc.transformer instead... What is the correct way to replace it? code: import…
Sweta Jain
  • 3,248
  • 6
  • 30
  • 50
8
votes
1 answer

Flutter/Dart - Cannot modify unmodifiable map error

Ive created an app with Flutter bloc package.Ive created a screen named learning_dashboard_screen which contains a list fetched from a server. inside that same screen I have a filter button which on press will show a filter screen using the bottom…
Kavishka Rajapakshe
  • 537
  • 1
  • 8
  • 23
8
votes
3 answers

Flutter Application Bloc listener not receiving state updates

I'm using flutter Bloc to navigate the user toward either the login page or home screen depending on whether they are authenticated or not. However, after the initial state change, the listener doesn't trigger when I change my authentication…
Niek
  • 187
  • 1
  • 8
8
votes
1 answer

how to implement WebSocket with clean architecture and bloc library flutter?

i'm trying to implement WebSocket in flutter using clean architecture my problem is how to get data from data source cuz in the case of using rest APIs you simply request and await data and get it from the data layer but in real-time apps such as…
reza47
  • 650
  • 6
  • 16
8
votes
2 answers

The superclass 'Bloc' doesn't have a zero argument constructor in dart

I am a beginner in Dart language development. I try to create a sample flutter application BLOC pattern inspired by this GitHub repo, but I got some error message related to the class inheritance. I am already familiar with the inheritance and…
Ragesh P Raju
  • 3,879
  • 14
  • 101
  • 136
8
votes
2 answers

Flutter BlocListener executed only once even after event gets re-fired

I am implementing Reso Coder's clean architecture in flutter. I followed his guides in dividing the project to layers and using dependency injection. In one of the cases I want to have the following scenario: An administrator user logs in, sees data…
8
votes
2 answers

How to validate form when submit in flutter with flutter_bloc?

This is my change password screen. I was using flutter_bloc for implementing mvvc pattern. This page works fine with bloc. But what I am trying to achieve is validate form when submitting the form. As I was new to flutter I have no idea how to do…
Dheepak S
  • 207
  • 2
  • 5
  • 17