【问题标题】:can I use multiple repositories in flutter bloc pattern我可以在flutter bloc模式中使用多个存储库吗
【发布时间】:2020-03-06 23:03:19
【问题描述】:

您好,我是 Flutter 的新手,正在尝试实现 bloc 模式。我在这些教程中检查了很多教程。 this is one of them. 他们总是为小示例项目使用单个存储库,如果我们在大型项目中使用单个存储库,那么存储库将变得非常混乱,所以我有疑问

我可以使用带有 bloc 模式的多个存储库吗?如果是,那么如何如果不是,那么管理存储库的最佳方法是什么,这样它就不会变得混乱?

【问题讨论】:

标签: flutter dart bloc


【解决方案1】:

为您的 BloC 创建多个存储库没有任何限制。 BloC 模式中的存储库只是包含将通过 Bloc 调用的提供程序的类。例如

class Repository {
  final albumApiProvider = AlbumApiProvider();

  Future<List<AlbumModel>> fetchAllAlbums() => albumApiProvider.fetchAlbum();
}

...在一个 BloC 中,可以调用该存储库,例如

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);
  }
  ...
}

这是一个使用简单 BloC 模式的应用程序的complete working sample,您可以尝试一下。

【讨论】:

    猜你喜欢
    • 2020-04-11
    • 2019-11-20
    • 2021-05-07
    • 2021-02-28
    • 1970-01-01
    • 2019-05-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-09
    相关资源
    最近更新 更多