nbr 2.1.6 nbr: ^2.1.6 copied to clipboard
Generic implementation of the network-bound-resource algorithm in Dart
nbr #
nbr
(Network Bound Resource) is a Dart library for managing resources that are bound to network requests. It simplifies the handling of data fetching from network and local storage, ensuring a consistent and reactive approach to resource management.
Features #
- Manages network requests and local storage seamlessly.
- Emits stream updates for different states: loading, success, failed, and empty.
- Provides a structured way to handle data fetching, caching, and mapping.
Installation #
Add nbr
to your pubspec.yaml
file:
dependencies:
nbr: latest
Then, run pub get
to install the package.
Usage #
Import the library:
import 'package:nbr/nbr.dart';
Example #
class MyNetworkBoundResource extends NetworkBoundResource<MyEntity> {
Future<void> fetchData() async {
await fetch<MyDTO>(
fetchFromAPI: () async {
// Implement your API call here
return MyDTO();
},
loadFromDB: () async {
// Implement loading data from the database
return null;
},
storeToDB: (data) async {
// Implement storing data to the database
},
shouldFetch: (data) async {
// Implement your logic to decide whether to fetch from API
return true;
},
mapDTOToEntity: (dto) {
// Implement mapping from DTO to entity
return MyEntity();
},
);
}
}
void main() async {
final resource = MyNetworkBoundResource();
resource.stream.listen((resource) {
if (resource.isLoading) {
print('Loading...');
} else if (resource.isSuccess) {
print('Success: ${resource.data}');
} else if (resource.isFailed) {
print('Failed: ${resource.error}');
} else if (resource.isEmpty) {
print('No data');
}
});
await resource.fetchData();
}
API #
NetworkBoundResource<Entity>
#
Abstract class representing a resource bound to a network request.
Properties
Stream<Resource<Entity>> get stream
: A stream ofResource
objects representing the current state of the resource.
Methods
-
Future<void> fetch<DTO>(...)
: Fetches the resource from the network and emitsResource
objects representing the current state.fetchFromAPI
: A callback to fetch the resource from the API.loadFromDB
: A callback to load the resource from the database.storeToDB
: A callback to store the fetched resource to the database.shouldFetch
: A callback to determine if the resource should be fetched from the API.mapDTOToEntity
: A callback to map the DTO from the API to an entity.
-
void dispose()
: Disposes of theNetworkBoundResource
by closing theStreamController
.
Resource<T>
#
A class representing the state of a resource.
Properties
bool get isLoading
: Indicates if the resource is in a loading state.bool get isSuccess
: Indicates if the resource has been successfully fetched.bool get isFailed
: Indicates if the resource fetching has failed.bool get isEmpty
: Indicates if the resource is empty.T? data
: The data of the resource.Object? error
: The error occurred during fetching the resource.
Static Methods
Resource<T> loading([T? data])
: Creates a loading state.Resource<T> success(T data)
: Creates a success state.Resource<T> failed(Object error)
: Creates a failed state.Resource<T> empty()
: Creates an empty state.