warped_bloc 0.0.4
warped_bloc: ^0.0.4 copied to clipboard
BLoC Utilities to make your life easier when you want to perform API Calls
Normally, when we use "bloc" as a state management solution we use it to fetch some data.
The Logic for GET request and POST request is quite similar. All you want are :
- LOADING STATE
- DATA STATE (Where you get data from GET request or success|fail for POST request)
- ERROR STATE (In case some error occurs)
The point is, we end up repeating these states all over the place if we want to create and app with BLoC pattern. Packages like 'freezed' try to help but the code generation is just too boring.
So, this package is a solution to that problem.
Features #
-
Introducing
AsyncCubitto deal with all of your API related states easily.It is pre-baked with
INITIAL,LOADING,FAILEDstates so you don't have to declare then again and again and again.To use it :
// DataState<T>() is from this package class MyBlocState extends DataState<List<Data>> { MyBlocState(List<Data> data) : super(data: data); } class MyBloc extends AsyncCubit { fetch() { // LoadingState() is from this package emit(LoadingState()); try{ var data = await someApiCall(); emit(MyBlocState(data)); }catch(e) { // ErrorState() is from this package emit(ErrorState(message: e.toString())); } } } -
BlocWrapperto finally get rid of having to remember when to useBlocListener,BlocBuilderorBlocConsumer.
If you give it abuilderas :BlocWrapper( builder: (context, state) => SomeWidget(), )then, it behaves as
BlocBuilder.
If you give it a
listenerwithchildas :BlocWrapper( listener: (context, state) {...}, child: SomeWidget(), ),then, it behaves as
BlocListener.
If you give it
builderandlistenerandchildas :BlocWrapper( listener: (context, state) {...}, builder: (context, state) => SomeWidget(), child: SomeOtherWidget(), )then, it behaves as
BlocConsumerandignores"child" parameter.
-
No need to use
if elsewhen dealing withAsyncCubitstates.For
builderparameter, usedefaultBuilderas :BlocWrapper<MyBloc, BlocState>( builder: defaultBuilder<BlocState, List<Data>, String>( onLoading: () => LoadingWidget(), onData: (List<Data> data) => ListingWidget(data), onError: (state) => ErrorWidget(state.message), ) )For
listenerparamter, usedefaultListeneras :BlocWrapper<MyBloc, BlocState>( listener: defaultListener<BlocState, List<Data>, String>( onLoading: (context) {...}, onData: (context, List<Data> data) {...}, onError: (context, state) {...}, ), child: ..., )
Getting started #
Add this package to your pubspec.yaml to start using it right now.
Usage #
More Usage detail in example.
Additional information #
Will be releasing blog on using this package soon.