state_bloc 0.0.4 copy "state_bloc: ^0.0.4" to clipboard
state_bloc: ^0.0.4 copied to clipboard

outdated

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.

state_bloc #

pub package

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 :  0.0.4

For help getting started with Flutter, view the online documentation.

Usage Example

import 'package:state_bloc/state_bloc.dart';

Define a StateBloc #

//Requires a initial value in it's constructor 
StateBloc<int> counterBloc = StateBloc<int>(0);

Listen to Data using BlocBuilder #

//Define the Generics eg : int, String, Foo.
BlocBuilder<int>(
  stateBloc: counterBloc,
  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

});

Managing App State with get_it #

In your flutter project add the dependency:

get_it: 3.1.0

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.

2
likes
0
pub points
0%
popularity

Publisher

verified publisherashishrawat.dev

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.

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

flutter

More

Packages that depend on state_bloc