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
6
votes
2 answers

How to make a bloc global accessible

I am struggling to put the block pattern in my Flutter App. I am using this library to make it a little bit simpler for me: https://felangel.github.io/bloc/#/ I have one main Question. How can I make my Bloc globally accessible without passing it…
Cornelius
  • 306
  • 4
  • 14
6
votes
1 answer

How to filter a list of obseravble in rxdart

I am trying to implement bloc pattern in rxdart . I am trying to build todo app type of app . I implemented showing all items in list but what I want is not to show completed and uncompleted items in different part . However I am not able to filter…
ashok poudel
  • 703
  • 11
  • 28
6
votes
3 answers

Flutter- How to hold data in memory throughout app lifetime in flutter?

I wish to know, how to hold data which I am using in multiple screens. Should we use the Singleton design pattern in a flutter? Suppose I made login module using BLoC pattern like here…
Ankur Prakash
  • 1,417
  • 4
  • 18
  • 31
6
votes
1 answer

BottomNavigationBar page change causing StreamBuilder data reload

Inside BottomNavigation I have 2 page. 1st Page loading data from the network and showing in SliverList and In another page showing static list data. After moving from 1st to 2nd page then 1st page all network data are gone. I have used…
Yeahia2508
  • 7,526
  • 14
  • 42
  • 71
5
votes
1 answer

Flutter bloc chat is not refreshing after sending message

I'm new to bloc and trying to implement a chat with flutter_bloc package. The service for my messaging is twilio conversations api. My function works perfectly fine, Im simply not able to refresh my list of messages. Could somebody show me what I'm…
5
votes
1 answer

Is it a bad practice if my bloc's state is a list of widgets?

I am using flutter_bloc package. Is it a bad practice if I have a bloc whose state is a list of widgets? I use this bloc to push (add) and pop (remove) widgets/mini-screens from a list. I use this list the body of a popup menu, which has something…
Iván Yoed
  • 3,878
  • 31
  • 44
5
votes
1 answer

Flutter: Cannot use this MethodChannel before the binary messenger has been initialized when running a function using isolates

I'm trying to use isolates in my Flutter app with Bloc as a state management solution. Right now, I'm running into an error when running a particular function in wallet_list.dart. // wallet_list.dart final FlutterSecureStorage _storage = const…
neil_ruaro
  • 368
  • 3
  • 15
5
votes
3 answers

How to manage multiple state on same screen using flutter bloc

i have a screen with three widget [widgetA, widgetB, widgetC] and i have a bloc[BlocA] which is responsible for the data fetching and displaying on this screen i have three event [eventA, eventB, eventC] which render the widget [widgetA, widgetB,…
avinash
  • 501
  • 2
  • 8
  • 16
5
votes
3 answers

How to update screen A when navigating back from screen B with bloc flutter

I have a screen(A) which use 'AccountFetched' state to load list of data from database: return BlocProvider( create: (context) { return _accountBloc..add(FetchAccountEvent()); }, child: BlocBuilder
noyruto88
  • 767
  • 2
  • 18
  • 40
5
votes
4 answers

How to call a function in cubit class in initState?

So, I have made a function in Cubit Class it is to get data from API. For now I just can get the data if I pressed a button. I wanna make the function automatically called when the page/screen is open. For your information, this page is the first…
5
votes
1 answer

flutter BlocProvider Navigation

suppose that we navigate to "PageA" using the following code: Navigator.push( context, MaterialPageRoute( builder: (context) { return BlocProvider( create: (context) => BlocA(), child: PageA(), ); }, …
reza
  • 1,354
  • 1
  • 7
  • 25
5
votes
1 answer

How to use Freezed package with Bloc in flutter?

I want to create a simple bloc with freezed package. This is my bloc: import 'package:bloc/bloc.dart'; import 'package:freezed_annotation/freezed_annotation.dart'; import 'package:presentation/presentation_index.dart'; part…
Salma
  • 1,211
  • 2
  • 16
  • 33
5
votes
1 answer

BuildContext in a Flutter BLoC class

In short, does the context belong in the BLoC class and if it doesn't, what's the right approach? I'm using a Provider as an abstraction layer between the Firebase DB and the UI. Recently, we've been abstracting further away to use the BLoC pattern,…
Dmitri Borohhov
  • 1,513
  • 2
  • 17
  • 33
5
votes
3 answers

What is a zero argument constructor in Dart flutter

What is a zero-argument constructor in Dart flutter? while creating a bloc in flutter I am receiving the following error The superclass 'Bloc' doesn't have a zero argument constructor. Try declaring a zero argument…
Javeed Ishaq
  • 6,024
  • 6
  • 41
  • 60
5
votes
1 answer

Best practice for BLoC pattern in Dart/Flutter

When using a the BLoC pattern in Flutter, what is considered good programming practice when it comes to structuring your application code? This is a loose question so I will try to give an example. Let's say that you define a BLoC class to handle…
spuriousGeek
  • 131
  • 1
  • 3
  • 9