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
0
votes
0 answers

Moving my existing flutter project to bloc architechture

The project assigned to me is a huge project with multiple screens and widgets. This project does not follow any kind of architecture. We are facing many issues related to state management and decided to move the project to bloc. Now my question is…
Nibha Jain
  • 7,742
  • 11
  • 47
  • 71
0
votes
0 answers

How to re-trigger initial event in Bloc instead using initState?

Here's the thing, I use get_it as dependency injection, and so I have the code: final sl = GetIt.instance; Future init() async { sl.registerFactory( () => ABloc( services: sl(), ), ); [...] } class MyApp extends…
Daniel Roldán
  • 1,328
  • 3
  • 20
0
votes
1 answer

FlutterBloc - set state value as initial text in TextFormField

Please look at my sample code: @override Widget build(BuildContext context) { final selectedIssue = context.select((IssueDetailsBloc bloc) => bloc.state.selectedIssue) ?? InternalIssue(); return WillPopScope( onWillPop: () async { …
user1209216
  • 7,404
  • 12
  • 60
  • 123
0
votes
2 answers

Could not find the correct Provider above this MyApp Widget

So, I'm using BLoC and Provider packages in one app. In my 'moviesprovider.dart' I am fetching some data from my API which returns a json, when app is opening first time. How can I get access to Provider.of(context) from main.dart in MultiProvider?…
0
votes
1 answer

Home Screen Loads Twice

I have a native Splash Screen so no need to create a new one here and need to set LoginPage as default as well I have authenticated status than define which route to follow so I set onGenerateRoute: (_) =>LoginPage.route(), and it loads twice in…
0
votes
0 answers

Why does BlocListener call a state where I removed the whole widget tree?

Let me explain, I have two listeners, one in ScreenA, and another in ScreenB, however both have the same state that does different actions, example: ScreenA listener : (BuildContext context, StateA state) { if (state is SuccessB) { ... show…
Daniel Roldán
  • 1,328
  • 3
  • 20
0
votes
2 answers

How to keep the old data in same state while emitting the same state with different data and render them separately using different blocBuilders in UI

I have a bloc called specialityBloc which will fetch doctors according to their speciality from firestore by delegating it to a repository. The problem is when I want to fetch different doctors with different speciality and emit them from same…
BestOFBest
  • 43
  • 7
0
votes
2 answers

Flutter how to store sharepreferances value for localization in bloc

i have implemented localization with bloc pattern but now I wanna store the value of language in sharepreferences so that next time if the user already selected language it will skip select language flow and fetch it from local storage. this is my…
Sheikh Raj
  • 41
  • 1
  • 7
0
votes
1 answer

Flutter ListView is not visible after search

I am creating a seach page with a textfiel and a listview to show the search results. When I do a search (call the onChange ) the results doesn't apear. I know that the method do what I want but Im not able to show the result I am using bloc/cubit…
0
votes
2 answers

How to consume the entitie (model) in a widget with BLoC Flutter

I am working on an application that uses BLoC for state management, this is the situation: I have a SearchDelegate that has a model as DataType (SearchDelegate), this SearchDelegate consumes an API to get SearchResults. When I onTap()…
lasd
  • 26
  • 4
0
votes
1 answer

Login button will not reset state after second press with Futter/Dart/BLoC

I've been working on trying to build a login page with Flutter/Dart and BLoC. So far I've got a login page with two input fields for a username and password and a login button that is built off of rounded_loading_button.dart. On first pressing the…
VanCoon
  • 422
  • 4
  • 20
0
votes
1 answer

Not able to parse socket data using bloc consumer

I have a stock marketing project which uses web socket connection, so in order to get the continuous response I have been using bloc consumer("flutter_bloc"). But I am not able to parse the response from the socket , so is there any blog or any…
0
votes
1 answer

Having trouble calling Bloc.observer in main.dart

When I call Bloc.observer in main.dart, it doesn't work
sweeture
  • 25
  • 7
0
votes
1 answer

I am having trouble getting last emitted state with BlocBuilder in other widgets/classes in flutter using Bloc/cubit

I have a sign in cubit, which will take two parameters, email and password. then it will ask repository to give him response of that request. Before calling the function of repository I emit loading state, and when I received data from repository…
0
votes
0 answers

Run bloc only when list is empty

Why my function in initState - the loop doesnt work? I mean that I just wanna run function _newsBloc.add(GetCompanyList()); Only in first time app run when list of users is empty. I just wanna make it because when I'm changing activities to other…
vowoker
  • 13
  • 4
1 2 3
99
100