For example i have two blocs, Bloc A and Bloc B. Both Bloc's is calling different api's and when i open the screen or app Bloc A And Bloc B calls the api. Problem is sometimes Bloc A or Bloc B takes sometime to load so during this i display circular progress indicator. But it doesn't look good that is Bloc A builds successfully and displays the content and Bloc B still showing circular progress indicator. So how can i implement that is if both Bloc A & B successfully loads then display both content otherwise show only single circular progress indicator at the center. How can i achieve this ?
My way to go would be: Create additional getter in BlocA and BlocB, like BlocAStatus.
Then simply create a BlocListener on wheter BlocA or BlocB and display content only if Bloc itself is ready and the other Bloc either.
BlocBuilder<BlocA, BlocAState>(
builder: (context, state) {
if(state.blocBStatus == BlocStatus.ready) {
// display content
}
}
)
In order to properly communicate between BLoCs, reading Bloc-to-Bloc Communication documentation section should be helpful.
https://bloclibrary.dev/#/architecture?id=bloc-to-bloc-communication
Related
Maybe I don't understand the purpose of BloC or Provider but I'm confused as to why we would ever want to use them instead of using Flutter's built-in state management using the Stateful widget. I've just finished an app and can't remember a point where I wished I needed something more than the defaults. Can anyone clear things up for me?
There are a few reasons to use a BloC or Provider rather than Flutter's built-in setState:
BloC and Provider offer a more robust way to manage state.
BloC and Provider make it easier to update state across multiple widgets.
BloC and Provider can be used to manage async data.
BloC and Provider offer a more modular way to structure your code.
There are some case that you need BLoC to make you easier to define each state or condition that happening inside the application.
We will start discussing creating app like https://www.tokopedia.com/ (inspect element and go with phone size preview). You will see that between widget section tokopedia_ss there are some loading animations, and when the data load complete the widget loading animation changed to viewable widget (as user).
in bloc, you will make stateLoading(), stateComplete(data), stateFailed(data). and in the controller or screen you can describe what will happen when bloc state is stateLoading etc...
Creating this case with setstate is more complicated and make your code messy, also setstate will make phone render all the Build() code. not like BloC builder, you can define each widget or section.
So, when there are 10 sections, using Bloc you able to make it render each state but when using standard setstate it will render all the 10 sections in one time every state changes.
More information about BloC: article_about_BloC
Is it mandatory for every event added to the bloc to generate a state (concerning the principles of the BLoC pattern)?
Example --
User clicks on the "Share on Whatsapp" button
We add the event for it
We do the logic inside the bloc in an if (event is ShareOnWhatsapp) condition
We do not generate a state back cause the UI doesn't need a rebuild
Is this okay to do with BLoC?
Would really appreciate references!
So I have a Cubit that is listening to an audio stream. When I first run load the page, the bloc builder rebuilds all of my UI as expected.
I then navigate away from the page, and try to resume activity. I am able to call a cubit function to trigger the hardware stream. My cubit is receiving that stream of data. I am able to print out logs from the hardware stream from within my cubit listener function, but when I call emit(NewState), the bloc builder doesn't rebuild.
There are many times where we would have had separation of concerns when it comes to UI code and business logic. This is reflected in most of the state management solutions that we use in Flutter.
In many of the state management solutions where the business logic is outside the view file ( widget file) , there are cases where we want to show the snackbar based on the logic in flutter.
But to show a Snackbar we first need a context object. I have used a NavigatorState to navigate without context. But I don't know if there is a way to show SnackBar without context.
Can someone provide a solution for this ?
Also it would be very helpful if you could provide a Utility method that manages the state internally and whenever user wants to show snackbar would just call that utility method from his logical part of code.
You can specify scaffoldMessengerKey as a property of MaterialApp and then ScaffoldMessenger can be directly used without BuildContext.
I think you can do the same way as navigatorKey.
ref: https://api.flutter.dev/flutter/material/MaterialApp/scaffoldMessengerKey.html
Check this answer for more details on how to implement this into your application, with some code examples.
You definitelly can show a piece of information without a context, something similar to a snackbar, however, it is not a snackbar.
If you use the package Get, you can navigate without context, open dialogs, snackbars or bottomsheets from anywhere in your code. However, the snackbars in particular have some issues.
As it doesn't check for a Scaffold, the snackbar is shown directly in the bottom of your screen. This means that if you have a BottomNavigationBar, it will be covered or partially covered by it.
It also means that if you have a FloatingActionButton, it won't be responsive to the snackbar position, as opossed to the normal snackbar.
So to sum up, with Get, you can have snackbars without context but you will have to make some sacrifices in your UI.
Example
final bottomSnackBar = GetBar(
isDismissible: false,
showProgressIndicator: true,
message: 'Processing...',
);
bottomSnackBar.show();
somethingAsync().then((_) => bottomSnackBar.hide());
So I am reading through Bloc for state management for flutter.
Since Bloc allows you to sink and stream (rebuilding a widget based on the input), then is it possible to build an app mostly with stateless widgets?
As an example, let say I make lots of single stateless class widgets, thus almost everything is compartmentalized into its own stateless widget.
With the Bloc state management, I could simply rebuild a certain stateless child widget to reflect the change.
In this approach, I don't see the need of using the stateful widget. Of course, being a total beginner in flutter, I wanted to hear if this approach has any merit to it.
Is this a good approach? Any info will be much appreciated.
You're right that you can use only StatelessWidgets. You just need to be cognizant of where you create your bloc. Some ways of instantiation are more easily testable than others, like passing the bloc to your StatelessWidget as an argument.
But for implementation, I like the flutter_bloc library the best:
https://pub.dev/packages/flutter_bloc
It includes BlocProvider which automatically handles creation and disposal of blocs.
One other thing to note is that you'll often have to kick off an event in a bloc to perform some action and a StatefulWidget could be useful to run that in the initState method.
You could either say in a StatefulWidget:
initState(){
_myBloc = SomeBloc()..add(SomeEvent());
}
// Then somewhere in your widget tree
BlocProvider<MyBloc>(
create: (context) => _myBloc,
builder: (context, state) {},
)
OR, in your StatelessWidget:
BlocProvider<MyBloc>(
create: (context) => MyBloc()..add(SomeEvent()),
builder: (context, state) {},
)
You'll find what works best for you, but I've found with Flutter that it mostly depends on the situation and goal of a particular feature. There's no need to pin yourself into a habit of always needing to use a StatelessWidget, but you are right that it is possible.
You can use only Stateless Widget. But there is one problem that you should close streams before the app is disposed of. It can be handled in two ways:
First, you can use a Stateful widget and close streams of bloc in the dispose method of stateful.
Using BlocProvider. In this case, Bloc Provider is a Stateful widget only. It closes streams automatically. Then you can use bloc using BlocProvider in Stateless Widget.
But it doesn't mean that we don't need stateful widgets. Stateful widgets are important in animation for example. Animation, text input or any local changes in the widget itself shouldn't be handled in bloc or other state management. it is the duty of widget itself.