Package that facilitates REST API and other network calls using flutter_bloc
and hydrated_bloc
.
Before taking a look a this package, please visit the BLoC library page, in order to get familiarized with various concept related to this package.
Here's a list of all the components have been built inside this package, and how they interact with one another. If you need more information about how everything works, please take a look at the example folder provided with the project, or create a new issue.
The status of a request made by a Request
object inside this library can be represented using the RequestState
enum. This are the possible values for the status:
- RequestState.init
- RequestState.loading
- RequestState.loaded
- RequestState.error
The RequestRepository
object is the base of this package, and allows you to easily create a repository
object that handles all data related to any data request.
It makes use of a BaseService
object. This way, the 'request' class logic is completly separated from the downloading and managing aspect of the information it controlls.
The loadData
is the method inside the 'request' object that handles all the event-emiting logic related to a repository
object. Inside it, you can call the fetchData
method you're using at that time.
You can use the autoLoad
constructor parameter to automatically call the loadData
method upon its initialization.
class TodosRepository extends RequestRepository<TodosService, String> {
TodosRepository(TodosService service)
: super(
service,
autoLoad: false,
);
@override
Future<String> fetchData() async => (await service.getTodos()).toString();
void loadError() {
emit(RequestState.error('ERROR GENERATED!'));
}
This object is pretty similiar to the one above, with the difference that it has te ability to make its data 'persistant', using the hydrated_bloc
library.
It also has the same front-end API as RequestRepository
.
class TodosRepository extends RequestPersistantRepository<TodosService, String> {
TodosRepository(TodosService service) : super(service);
@override
Future<String> fetchData() async => (await service.getTodos()).toString();
void loadError() {
emit(RequestState.error('ERROR GENERATED!'));
}
This widget makes contruction UI using Request
object pretty easily. It has a RequestWidgetBuilder
parameter for each RequestState
value, so that the interface can adapt to the current status of the request.
RequestBuilder<TodosCubit, String>(
onInit: (context, state) => Text('Hello world!'),
onLoading: (context, state, value) => CircularProgressIndicator(),
onLoaded: (context, state, value) => Text(value!),
onError: (context, state, error) => Text(error!),
)
If you want to take a deeper look at the example, take a look at the example folder provided with the project.
This project is a starting point for a Dart package, a library module containing code that can be shared easily across multiple Flutter or Dart projects.
For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.
- Flutter - Beautiful native apps in record time.
- Android Studio - Tools for building apps on every type of Android device.
- Visual Studio Code - Code editing. Redefined.
This project is licensed under the GNU GPL v3 License - see the LICENSE file for details.