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

Stream/Bloc/Repository/Firebase data flow Flutter

I'm starting with flutter as I want to port my swift app to Flutter, but I'm getting stuck understanding the pattern Bloc/Repository/Firebase as I'm following the tutorial https://bloclibrary.dev/#/flutterfirestoretodostutorial dough I use the real…
Vincenzo
  • 5,304
  • 5
  • 38
  • 96
7
votes
1 answer

Bloc Pattern: Every screen gets its own bloc?

I am learning the bloc pattern for Flutter and there seems to be a recurring piece of advice that "every screen should have its own bloc". But what if you queried your server for data that will be used in more than one screen? It seems redundant,…
Matt
  • 463
  • 4
  • 14
7
votes
1 answer

Animated Switcher and Bloc Builder

im new to bloc pattern in flutter. One of my states classes have a list of widget and an index as a field. My goal is to update the child of an Animated Switcher using this state's widgets. return AnimatedSwitcher( duration:…
7
votes
2 answers

access BlocProvider.of(context) in dispose method

does anyone have any idea of how I can do this? My code: @override void dispose() { final FiltersBloc filtersBloc = BlocProvider.of(context); super.dispose(); } error is: flutter: BlocProvider.of()…
wei
  • 557
  • 1
  • 10
  • 24
7
votes
1 answer

Repository provider in the flutter_bloc library doesn't provide repository with when pushing new route

I am using the flutter_bloc library to architect my app. In addition to the BlocProvider I am using the Repository Provider, since I will be using a specific repository extensively throughout my app. But I am having an issue with regards to context…
DanT29
  • 3,069
  • 4
  • 24
  • 32
7
votes
1 answer

BLoCs and multiple streams - Is there a better solution?

Currently I'm working with BLoCs in Flutter and I've got a question about multiple streams within a Bloc. For example when a screen has got multiple widgets which should depend on the Bloc. I could wrap the whole screen in the StreamBuilder, but…
Daniel
  • 1,999
  • 4
  • 15
  • 27
7
votes
1 answer

How to manage a stream of List of objects in Flutter?

I've been dabbling with flutter for a few days, and I'm trying to make a simple ToDo app, as a learning project. I'm trying to implement something like a BLoC. A list of ListItem widgets is built with ListView.builder, wrapped in StreamBuilder. I…
Jayasurya
  • 758
  • 1
  • 6
  • 12
7
votes
2 answers

Flutter Bloc: BlocBuilder not getting called after an update, ListView still displays old data

I'm using flutter_bloc for state management and landed on this issue. When updating a field and saving it, the BlocBuilder is not refreshing the page. It is working fine when Adding or Deleting. I'm not sure what I'm doing wrong here. Even if I go…
Kajan Nallathamby
  • 287
  • 1
  • 2
  • 5
7
votes
1 answer

how to get multiple streams based on the same stream controller in flutter?

I have a Bloc class that needs three streams based on the same stream controller. class TodoBloc { final _todoController = StreamController>(); get allTodoStream => _todoController.stream; get activeTodoStream =>…
Salma
  • 1,211
  • 2
  • 16
  • 33
7
votes
1 answer

StreamBuilder throws Dirty State saying Invalid Arguments

I am trying the bloc pattern for my flutter app. I want to change colour of text field when the stream value changes. Following is my bloc code class Bloc { final StreamController _changeColor = PublishSubject(); Function(bool) get…
BraveEvidence
  • 53
  • 11
  • 45
  • 119
7
votes
1 answer

How to do backend validation using BLOC pattern in Flutter TextField?

I want to create a TextField that check if the value exist in database. How to do async validation using BLOC pattern with TextField widget? Should I use StreamTransformer to add error to the Stream? I tried using DebounceStreamTransformer but it's…
6
votes
2 answers

How to pass a bloc to another screen using go_router on flutter

On Flutter I would like to pass a local bloc to another screen. This is how I used to pass a bloc to a new route when using the default navigator. Navigator.of(context).push( MaterialPageRoute( builder: (BuildContext context) => …
Abdi mussa
  • 171
  • 11
6
votes
4 answers

( Flutter Bloc ) doesn't conform to the bound 'StateStreamable' of the type parameter 'B'

why am i getting the error? I couldn't find the reason. I'll be happy if you can help me. error screen You can check my issue on github
Serkan Ağca
  • 155
  • 1
  • 6
6
votes
1 answer

Flutter bloc new version deprecated mapEventToState

I have a bloc hierarchy, where in the child bloc's mapEvenToState I used super.mapEventToState. In the newer version of the bloc package, mapEventToState is deprecated. What should I use instead of super.mapEventToState? I know about on, but…
Errr
  • 65
  • 1
  • 4
6
votes
1 answer

Flutter - How can I pass a Bloc as a parameter in a reusable Widget?

I'm building a login page, and I have created what I intend to be used as a fully dynamic TextField widget. I now need to create 2 instances of that widget (email and password inputs) and send parameters for hintText, errorText, and then my…
solacking
  • 317
  • 2
  • 9