state_bloc
StateBloc help the Widget tree to rebuild again with the help of streams. You do no need to create any bloc class for managing state, just need to instantiating the StateBloc class with an initial value.
Getting Started
In your flutter project add the dependency:
state_bloc : 1.0.1
For help getting started with Flutter, view the online documentation.
Usage Example
import 'package:state_bloc/state_bloc.dart';
StateBloc
Define a//Requires a initial value in it's constructor
StateBloc<int> stateBloc = StateBloc<int>(0);
Listen to Data using StateBlocBuilder
You will need to pass the stateBloc
and a widget
to StateBlocBuilder
//Define the type of data you are using in StateBloc eg : int, String, Foo.
StateBlocBuilder<int>(
stateBloc: stateBloc,
widget: (int value) {
return Text("$value");
},
)
Change the state of the bloc
1. Change the state when you have no access of the value
This will provide you the previous state data present in the bloc other vise the state you have provided while instantiating the StateBloc
stateBloc.changeState(state: (value) {
//do changes with value and return it
return value;
});
2. Change the state without value provided
stateBloc.changeStateWithoutValueProvided(value);
Listen to the value changed
stateBloc.listenToValue(listenToValue: (int value) {
//listen to the value when changed
});
Don't forget to dispose the bloc
stateBloc.dispose();
get_it
Managing App State withget_it Package by escamoteur
In your flutter project add the dependency:
get_it: <latest-version>
Register the StateBloc with GetIt.
GetIt appConfig = GetIt();
void setupAppConfig() {
//This will create a Singleton for the whole app
appConfig.registerLazySingleton(
() => StateBloc<DarkThemeModel>(DarkThemeModel()));
}
if you want to register the another StateBloc with GetIt , you will have to provide the instance name.
GetIt appConfig = GetIt();
void setupAppConfig() {
appConfig.registerLazySingleton(
() => StateBloc<DarkThemeModel>(DarkThemeModel()), "darkMode");
}
Register when the app runs.
void main() {
setupAppConfig();
runApp(MyApp());
}
Using StateBloc instance from the GetIt
StateBloc<DarkThemeModel> stateBloc= appConfig<StateBloc<DarkThemeModel>>("darkMode");
Kaboom
Now you can change the app state with StateBloc with GetIt.