can I use multiple repositories in flutter bloc pattern
Hi I am new with flutter and trying to implement bloc pattern. I have checked so many tutorials in those tutorials. this is one of them. they always using a single repository for small sample project and if we use single repository in a large project then the repository will become very messy so I have a doubt
can I use multiple repository with bloc pattern If yes then how If no then what's is best way to manage repository so that it won't become messy?
Solution 1:
There's none limiting you on creating multiple repositories for your BloC. Repositories in a BloC pattern are just classes containing Providers that will be called through Bloc. For example
class Repository {
final albumApiProvider = AlbumApiProvider();
Future<List<AlbumModel>> fetchAllAlbums() => albumApiProvider.fetchAlbum();
}
...and in a BloC, that Repository can be called such as
class AlbumsBloc {
final _repository = Repository();
final _albumFetcher = StreamController<List<AlbumModel>>();
Stream<List<AlbumModel>> get allAlbums => _albumFetcher.stream;
fetchAlbum() async {
List<AlbumModel> listAlbums = await _repository.fetchAllAlbums();
_albumFetcher.sink.add(listAlbums);
}
...
}
Here's a complete working sample of an app using a simple BloC pattern that you can try out.