common_bloc 0.1.0 copy "common_bloc: ^0.1.0" to clipboard
common_bloc: ^0.1.0 copied to clipboard

outdated

A collection of common blocs that maybe will be used very often (powered by the bloc library).

common_bloc #

A collection of common blocs that maybe will be used very often.

Idea of this #

Sometimes we need to create various blocs in the same project to do almost the same thing. This is a collection of blocs to no write so many code.

Powered by #

Blocs #

RestBloc #

This is a bloc to make network requests over a REST API.

Usage

To use it, we need to create an instance:

final restBloc = RestBloc('<baseUrl>'); //BaseURL of REST API.

Also, we can pass a list of interceptors:

final restBloc = RestBloc('<baseUrl>', interceptors: []);

Uses the InterceptorContract class of http_interceptor package to listen to request and response.

After that, we can use the following methods:

restBloc.get('path'); // With optional params as a Map<String, String>.
restBloc.post('path', body: body); // With body as a String.
restBloc.put('path', body: body); // With body as a String.
restBloc.patch('path', body: body); // With body as a String.
restBloc.delete('path');

In all of the methods (except 'delete'), we have an optional param named: fromJson

restBloc.get('path', fromJson: fromJson);

This must be a Function to convert the response into an object. Expects to work with json_serilizable and BuiltValue

restBloc.get('path', fromJson: Data.fromJson);

In that, the 'data' object will be of that type.

States

As a bloc, we have states to manage the request and because is powered by freezed, we can use something like:

BlocBuilder<RestBloc, RestState>(
  bloc: restBloc,
  builder: (context, state) => state.when(
    uninitialized: () => Container(), //Widget for UninitializedState
    loading: () => Container(), //Widget for LoadingState
    error: (error) => Container(), //Widget for ErrorState
    loaded: (data, lastPath, timestamp) => Container() //Widget for LoadedState
  )
);

RequestBloc #

This bloc will be used when we want to make a task, a request, o something that needs a response/data/result.

Usage

This bloc doesn't need params to be constructed.

final requestBloc = RequestBloc();

When we need to perform an action, we call:

requestBloc.perform(action, actionName);

'action' is a function that returns a Future to be resolved and will be the result.

'actionName' is a identifier of what we are doing.

requestBloc.perform(() async => 'Hi!', 'Waving');

States

This use the same states of RestBloc:

  • uninitialized
  • loading
  • error
  • loaded

In the future #

  • Tab Bloc (for controlling tab behaviour).
  • Session Bloc.
  • Login/Register Bloc.
  • A suggestion?.
10
likes
0
pub points
4%
popularity

Publisher

verified publisherpixela.tech

A collection of common blocs that maybe will be used very often (powered by the bloc library).

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

bloc, flutter, freezed_annotation, http, http_interceptor, meta

More

Packages that depend on common_bloc