reusable_bloc 1.0.4
reusable_bloc: ^1.0.4 copied to clipboard
A generic bloc to fetch(and/or refetch) network data easily.
Generic Refreshable Data Blocs which are easily extendible to fetch data from network
Features #
- implement whole bloc by extending the
DataBloc
class and overriding the fetch data function - implements optionally to refresh the data, calling the fetch the data again
- remembers the previous data when on refresh error and rollback to
DataLoaded
State again - uses
Equatable
class fromequatable
package to add comparison of the state class - the fetch data function returns
Either<Failure,Data>
fromdartz
package, making it fully compatible with clean-architecture
Getting started #
- This generic bloc package is an extended and flexible version from this blog-part1 and blog-part2. If you want to learn bloc as a state machine, head on to this blog.
Usage #
class GreetBloc extends DataBloc<String> {
GreetBloc() : super(DataUninitialized());
@override
Future<Either<Failure, String>> fetchAndParseData<Failure>(
DataState<String> oldState, FetchData event) async{
final p = event.param as GreetFetchParam;
await Future.delayed(Duration(seconds:2)); // network call place holder
return Right(p.name);
}
}
/// [FetchParam] is a class that holds the optional parameters for the [FetchData] event.
class GreetFetchParam extends FetchParam {
final String name;
const GreetFetchParam(this.name);
@override
List<Object?> get props => [name];
}
void main() {
final bloc = GreetBloc();
bloc.add(FetchData<String>(GreetFetchParam('ajeet')));
bloc.stream.listen((event) {
print(event);
});
}
Additional information #
You liked this package? then hit a like. I don't want you to buy me a coffee. Just use this package if you feel the need. And if you need a feel... contribute and let's make something better together.
- Star this repository
- Create a Pull Request with new features
- Share this package
- Create issues if you find a Bug or want to suggest something