如何使用

graph
loading[LoadingWidget]
failure[FailureWidget]
child[ChildWidget]
bloc[Bloc extends BaseBloc]
repository[Repository]
interceptor[json decoder interceptor && token interceptor]
localDataSource[LocalDataSource]
remoteDataSource[RemoteDataSource]
bloc --futureWrapper ----> repository
bloc --helperBloc.isLoading--> loading
bloc --helperBloc.hasFailure--> failure
bloc --result of futureWrapper is success --> child
repository --> localDataSource
localDataSource --> database
repository --> remoteDataSource
remoteDataSource --> interceptor
interceptor --> server

  • 为您的应用程序创建 LocalDataSource,并使其依赖于 BaseLocalDataSource(建议阅读 BaseLocalDataSource 部分)。

  • 为您的应用程序创建 RemoteDataSource,并继承自 BaseRemoteDataSource(建议阅读 BaseRemoteDataSource 部分)。

  • 为您的应用程序创建 SharedPrefDataSource,并继承自 BaseSharedPreferences

  • 添加 TokenInterceptor 以利用令牌管理。

  • 添加 JsonDecoderInterceptor 以利用拦截器内部解码 JSON。

  • 创建您的存储库,并使其继承自 BaseRepositiory,然后开始使用 ** requestData** 函数进行数据源调用(建议阅读 BaseRepositiory 部分)。

  • 如果您将 Bloc 用作状态管理,请创建 BaseBlocPageState 的子类并重写必需的函数。

  • 对于您创建的每个 bloc,请继承 BaseBloc,这将为您提供辅助函数,以加快开发过程(建议阅读 Statemangment 部分)。

文件夹结构

- lib

	|---- datasource
	|		|
	|		|--- local_data_soruce
	|		|		|
	|		|		|-- i_base_local_data_source.dart
	|		|		|
	|		|		|-- base_local_data_source.dart
	|		|		|
	|		|--- remote_data_source
	|		|		|
	|		|		|-- i_base_remote_data_source.dart
	|		|		|
	|		|		|-- base_remote_data_source.dart
	|		|		|
	|		|--- shared_preferences_data_source
	|		|		|
	|		|		|-- i_base_shared_preferences.dart
	|		|		|
	|		|		|-- base_shared_preferences.dart
	|		|
	|---- repository
	|		|
	|		|--- base_repository.dart
	|		|
	|		|--- common_app_repository.dart
	|
	|---- presentation
	|		|
	|		|--- mixins
	|		|		|
	|		|		|-- bloc_builder_mixin.dart
	|		|		|
	|		|		|-- bloc_consumer_mixin.dart
	|		|		|
	|		|		|-- bloc_listener_mixin.dart
	|		|		|
	|		|		|-- bloc_provider_mixin.dart
	|		|		|
	|		|		|-- search_mixin.dart
	|		|		|
	|		|		|-- size_mixin.dart
	|		|		|
	|		|		|-- theme_mixin.dart
	|		|
	|		|--- widgets
	|		|		|
	|		|		|-- base_bloc_page.dart
	|		|		|
	|		|		|-- base_state_widget.dart
	|		|		|
	|		|		|-- bloc_helper_widget.dart
	|		|		|
	|		|		|-- bloc_state_getit.dart
	|		|		|
	|		|		|-- bloc_state_provider.dart
	|		|
	|---- state_mangement
	|		|
	|		|--- bloc_state_mangement
	|		|		|
	|		|		|-- base_bloc.dart
	|		|		|
	|		|		|--- helper_bloc
	|		|		|
	|		|		|	|-- helper_bloc.dart
	|		|		|	|
	|		|		|	|-- helper_state.dart
	|		|		|	|
	|		|		|	|-- helper_event.dart
	|		|
	|---- utils
	|		|
	|		|--- data_model_wrapper.dart
	|		|
	|		|--- token_constants.dart
	|		|
	|		|--- extensions
	|		|		|
	|		|		|-- double_ext.dart
	|		|		|
	|		|		|-- int_ext.dart
	|		|		|
	|		|		|-- map_ext.dart
	|		|		|
	|		|		|-- string_ext.dart
	|		|
	|		|--- failures
	|		|		|
	|		|		|-- base_failure.dart
	|		|		|
	|		|		|-- local_failures.dart
	|		|		|
	|		|		|-- network_failures.dart
	|		|
	|		|--- interceptors
	|		|		|
	|		|		|-- json_decoder_interceptor.dart
	|		|		|
	|		|		|-- token_interceptor.dart
	|		|
	|---- constant.dart
	|
	|---- type_defs.dart

数据层

  1. 失败

    • BaseFailure

      每个失败都必须实现的基类,有一个属性需要重写,即 failureMessage


      数据源
      • BaseRemoteDataSource

        • 反序列化器

          T Function(Object?) 如何将 JSON 解析为对象。

        • Future<DataModelWrapper> request

          此函数将执行 HTTP 请求,解析响应,并根据 HTTP 响应返回 DataModelWrapper。

        • failureParser(Response response)

          根据响应返回失败字符串。

        • defaultErrorMessage

          失败中的默认消息,如果无法进行 failureParser,则返回 null。

        • wrapBodyWithBaseRequest(data)

          用基类请求包装请求体。


        Repository

        graph LR
        A[Repository] --1- localCall --> B[LocalDataSource]
        A --2- remoteCall --> C[RemoteDataSource]
        C --3- data--> A
        A --4- saveRemoteDataFunction--> B
        

        • BaseRepository

          • Future<DataModelWrapper> requestData

            此函数将包装到数据源层的调用,并管理是从远程服务器还是本地数据库获取数据,并将结果缓存到 localdatabase

        状态管理

        • HelperBloc

          此 bloc 将提供三个主要功能:1- PageLoading。 2- ErrorHandling。 3- 为您的 bloc 提供 BuildContext

        • BaseBloc

          void runFunctionWithContext(ContextCallback contextCallback) 此函数将提供 BuildContext

          Future<DataModelWrapper> futureWrapper:此函数将包装您的 future 调用并管理页面加载和显示错误消息。

        表现层

        • Mixins

          • SizeMixin

            将为您的状态类提供主要的大小属性。

          • ThemeMixin

            将为您的状态类提供主要的主题属性。

          • SearchMixin

            将为您的状态类提供主要的搜索功能。

          • BlocProvidersMixin

            将在您的状态类上方注入 BlocProvider

          • BlocListenerMixin

            将在您的状态类上方注入 BlocListener

          • BlocConsumerMixin

            将在您的状态类上方注入 BlocConsumer

          • BlocBuilderMixin

            将在您的状态类上方注入 BlocBuilder


          拦截器
          • TokenInterceptor

            用于在请求中注入身份验证令牌的辅助拦截器。

          • JsonDecoderInterceptor

            用于将 JSON 从 String 转换为 Map<String, dyanmic> 的辅助拦截器

          GitHub

          查看 Github