bloc_repository 1.0.1
bloc_repository: ^1.0.1 copied to clipboard
An Additional Layer Of Abstraction For Creating Repositories Following The B Lo C Pattern Architecture
bloc_repository #
An additional layer of abstraction for creating repositories following the BLoC pattern architecture
Platform support #
| Android | iOS | macOS | Web | Linux | Windows |
|---|---|---|---|---|---|
| ✔ | ✔ | ✔ | ✔ | ✔ | ✔ |
Motivation #
The existing BLoC libraries are amazing for handling state managment in Flutter projects. However, the boundaries for creating Repositores is are way too loose. In package:flutter_bloc, a Repository can literally be any class. This package solves that problem by creating a class, Repository, which all the repositories can inherit from.
Another problem is repository-to-repository communication. A repository should be fairly independent of other repositories and their implementations. However, sometimes a repository needs to call a method in another repository. To decouple repositories from each other, this package contains a method channel which can be used to communicate between repositories.
Getting started #
-
Create a
RepositoryChannelclass MyRepositoryChannel extends RepositoryChannel{ // Add your custom repository events here MyRepositoryChannel({ required super.log, required this.myCustomCallback, }); final void Function() myCustomCallback; } -
Create
Repositoryand use the channelclass MyRepository extends Repository<MyRepositoryChannel>{ MyRepository({ required MyRepositoryChannel channel, }) super(channel); /// You can override the `initialize` and `dispose` methods, like so @override FutureOr<void> initialize(){ // Do own initialization super.initialize(); } Future<void> doSomething() async { channel.log('Starting to do something!'); // Do something channel.myCustomCallback(); channel.log('Finished doing something!'); } } -
Use the repository
Widget build(BuildContext context){ return RepositoryProvider<MyRepository>( create: (_) => MyRepository( channel: MyRepositoryChannel( log: (value) => print(value), myCustomCallback: () {}, ); ); ); }
