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

Failed assertion line 5120 pos 12: 'child = _child' is not true

I am trying to create a listview with API data using bloc pattern following is the error: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 5120 pos 12: 'child == _child': is not true. My list file: import…
Panks
  • 565
  • 1
  • 7
  • 20
13
votes
4 answers

Flutter: how to mock Bloc

I would like to mock my Bloc in order to test my view. For example, this is my Bloc: class SearchBloc extends Bloc { @override // TODO: implement initialState SearchState get initialState =>…
Little Monkey
  • 5,395
  • 14
  • 45
  • 83
12
votes
3 answers

Flutter how to get brightness without MediaQuery?

My goal is to create an app where the user can choose his preferred theme. I'm saving the user's choice with shared preferences so I can load it the next app start. The user can either select: - Dark Mode (Independent from the OS Settings) - Light…
Just A Question
  • 423
  • 6
  • 21
12
votes
1 answer

Is nested StreamBuilder good pattern?

I am fetching articles from HackerNews API using Bloc Pattern and Streams. I am loading all the articles and presenting in the UI with the help of a stream builder, and this works fine. Now I wrapped the article fetching Stream builder with the new…
Dhruvam Sharma
  • 1,661
  • 14
  • 22
12
votes
3 answers

How to give a default value to a textfield when using bloc framework

I've written some simple flutter apps which use stateful widgets / forms / textformfields to enter and manage data. Now I'm trying to understand how to use BLoCs and streambuilders to do similar work. But I can't work out how to set initial values…
user3249753
11
votes
2 answers

How to use flutter internationalization inside BLOC, Cubit And Repositories for translate messages?

I am working on a multilingual app in Flutter. I used flutter_localizations package for localization and intl package for internationalization. In the normal way inside Widgets, I can use it with help of Context. But the problem is when I want to…
Taleb
  • 1,944
  • 2
  • 11
  • 36
11
votes
4 answers

Flutter build called multiple times

I have a bottom nav bar that has a list page that uses a bloc for the state. class _MainPageState extends State { int _index = 0; @override Widget build(BuildContext context) { final List _widgets = [ const…
loki
  • 2,271
  • 5
  • 32
  • 46
11
votes
3 answers

Flutter Bloc does not change TextFormField initialValue

I'm using Bloc library and noticed after yielding a new state my TextFormField initialValue does not change. My app is more complicated than this but I did a minimal example. Also tracking the state it is changing after pushing the events. Bloc is…
loki
  • 2,271
  • 5
  • 32
  • 46
11
votes
1 answer

Nesting BlocBuilders to Manage Multiple States on the same Widget

I wanted to nest the text Update BlocBuilder inside another BlocBuilder that controls its font size through a slider widget. But, I can't get it to work. Please help! BlocBuilder( builder: (context, state)…
Jonathan Daniels
  • 169
  • 1
  • 3
  • 10
11
votes
1 answer

Flutter: can I use setState() with a BLoC architecture?

My understanding of state management is that calling setState() alone opens up a variety of messy issues, code files become huge and difficult to debug, and it prevents holding a sensible structure to a project. In cases where the widget's…
Tom
  • 1,739
  • 15
  • 24
11
votes
7 answers

flutter test fails. compiler error getter not found 'main'

I'm trying to run flutter test using the terminal and i receive the following error. the following file C:/Users/User/AppData/Local/Temp/flutter_test_listener.e6fa92b4-6cd1-11e9-b9cb-68f728ca4610/listener.dart doesn't exist in the directory…
Salma
  • 1,211
  • 2
  • 16
  • 33
11
votes
1 answer

Using a singleton for Bloc

I know that some of my BLOCs will be instantiated only once throughout the app lifecycle. Is it bad practice or anti-pattern to create these BLOC classes as a singleton. Singleton can help in accessing the BLOC anywhere and also makes sure that the…
Vamsi Krishna
  • 30,568
  • 8
  • 70
  • 78
10
votes
1 answer

Observing streams using Cubit (BLoC library)?

TL;DR: Is there a way to listen to streams and emit state using Cubit instead of BLoC? I'm using the BLoC library for Flutter and we use Cubits for our state management. Everything is working all right so far for interactions to get data or save…
kobowo
  • 2,529
  • 2
  • 24
  • 37
10
votes
4 answers

Flutter Bloc is not updating the state/ not working

I am developing a mobile application using Flutter. I am using the flutter bloc package, https://pub.dev/packages/flutter_bloc for managing and setting up the bloc. But when the state change it is not updating the widgets or views. I have a bloc…
Wai Yan Hein
  • 13,651
  • 35
  • 180
  • 372
10
votes
1 answer

Flutter- How to get last value from Stream without RxDart?

Need to know whether I can get last value from Stream without using third party library. The first​ way I tried, when I can sending the value to stream in 'changeEmail', I can store the newValue in some variable in my BLoC. Is it correct? The second…
Ankur Prakash
  • 1,417
  • 4
  • 18
  • 31