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
4
votes
1 answer

Flutter Bloc cancel running event when a new one is recieved

I have the follwing Bloc class DeviceBloc extends Bloc { DataRepository repository; DeviceBloc({@required this.repository}) : super(DeviceInitialState()) { on(onFetchResources); } …
Marc
  • 2,023
  • 4
  • 16
  • 30
4
votes
0 answers

How to make a second API call based on the first response in flutter with cubit?

There is a problem with the first call I have to get the value list of values from the first API to make a second API call. how do update the data to UI. I have two listviews one is for loading from the first API and the second listview is for the…
kumar
  • 111
  • 12
4
votes
1 answer

Using PageView with Bloc to programmatically 'swipe' pages. Prevent the bloc builder from building on certain states?

I've built a PageView and want to control page swiping programmatically using a Bloc. I'm using a PageController and listening for my bloc's ShowPage state. Then calling pageController.animateTo to animate the PageView to the desired page. The bloc…
Simon Hutton
  • 1,677
  • 3
  • 22
  • 35
4
votes
0 answers

Dependency Injection with RepositoryProvider with BLoC

Given I have a Repository: class Repository { final DataSource dataSource; Repsoitory(this.dataSource); } and a DataSource: class DataSource { final Dio dio; DataSource(this.dio); } and I would like to initialize Dio with these…
Stefan Galler
  • 781
  • 4
  • 12
4
votes
1 answer

How to call api once in futurebuilder

My application have different routes and I would like to know how to call my api with cubit just once when the user come for the first time on the screen and also not to re-call the api every time he returns to the screen already initialized. my…
Brain
  • 75
  • 1
  • 5
4
votes
2 answers

Flutter bloc is not rebuilding in 7.2.0 version with Equatable

I created simple app to test bloc 7.2.0 and faced that BlocBuilder doesn't rebuild after first successful rebuild. On every other trigger bloc emits new state, but BlocBuilder ignores it. Please note, if I remove extends Equatable and its override…
Preckrasno
  • 2,435
  • 1
  • 10
  • 20
4
votes
2 answers

how can i confirm password using formz in flutter

When I want to confirm my password, but i can not compare the first and last passwords. Here my password class. It works fine, but confirmPassword section not. class Password extends FormzInput { const…
alperefesahin
  • 604
  • 6
  • 22
4
votes
2 answers

hydrated_bloc: The parameter 'storageDirectory' is required

I updated hydrated_bloc from 6.1.0 to the latest 7.0.1 and I got a warning in: HydratedBloc.storage = await HydratedStorage.build(); The parameter 'storageDirectory' is required. When I changed to what the new documentation…
Abdullah Hamadi
  • 161
  • 3
  • 10
4
votes
1 answer

Flutter Navigator 2.0 does not transition between root navigator pages

I'm using Navigator 2.0 API to handle navigation in my app, and I have problem with top-most RouterDelegate (the nested RouterDelegates underneath it work fine). Depending on authentication state I want to show Splash Screen, Login Screen, or Home…
4
votes
0 answers

The name '_SignInFormState' isn't a type and can't be used in a redirected constructor. Try redirecting to a different constructor. flutter

I am new to bloc. My error is "The name '_SignInFormState' isn't a type and can't be used in a redirected constructor. Try redirecting to a different constructor. flutter."I generated freezer files. The issue still persists. part of…
Abin Sunny
  • 89
  • 1
  • 7
4
votes
1 answer

hydrated_bloc not persisting data

I'm building a flutter app that uses BLoC pattern and using hydrated_bloc package to persist data. The bloc itself is working fine, ie the event comes into the bloc and the bloc yields a state back to the ui. The issue is that the bloc is not saving…
Jason Lloyd
  • 378
  • 7
  • 23
4
votes
3 answers

can I use Getx and Bloc state management in one project ( Social Network app , big project ) without problems?

My project is a Social network hybrid mobile app that is made with flutter, dart ( frontend ) and Nodejs as a backend, the point that I hired front end developers that made part of the project ( 35 % of the project ) using GetX as state Management,…
4
votes
3 answers

Flutter BLoC: Are Cubits better then BLoC?

I'm working with Flutter quite a long time and have a bunch of released products. I never really liked BLoC and preferred to use Provider or later Riverpod. I just don't understand that event concept. Why do we still need it? And i'm confused…
Max Orlov
  • 73
  • 1
  • 8
4
votes
3 answers

Is it allowed to add event to another bloc from inside of a bloc?

I am using bloc library available in Dart to implement "bloc" pattern. I will eventually move onto flutter_bloc library so I can use it inside a real app. I'm having a bit of difficulty understanding how to create some general blocs that can be…
user3056783
  • 2,265
  • 1
  • 29
  • 55
4
votes
3 answers

Flutter Bloc State not updating when using copywith

Outline: Navigate to profile page Pass in user id to fetch doc from firestore Pass the retrieved data to state.copyWith(data:data) yield a successful status and present ui My problem is that when i am using state.copyWith(data:data) the state is…
StrandedYax
  • 83
  • 1
  • 6