init method

Future<void> init({
  1. required String baseUrl,
  2. Directory? cacheDir,
  3. String cacheName = 'app_local_data',
  4. List<Interceptor>? interceptors,
  5. BaseOptions? baseOptions,
  6. bool isDebug = false,
  7. bool useSystemPrint = false,
  8. String? printTag,
  9. CheckNetWork? baseCheckNet,
  10. List<String>? ignoreCacheKeys,
  11. RequestCaptureError? requestCaptureError,
  12. CacheMode? baseCacheMode,
  13. HiveCipher? encryptionCipher,
  14. Map<String, dynamic>? baseUrlEnv,
  15. int cacheInvalidationTime = 365 * 24 * 60 * 60 * 1000,
  16. double debugWindowWidth = 600,
  17. double debugWindowHeight = 500,
})

初始化公共属性 baseUrl 地址前缀 interceptors 基础拦截器

Implementation

Future<void> init({
  required String baseUrl,
  Directory? cacheDir,
  String cacheName = 'app_local_data',
  List<Interceptor>? interceptors,
  BaseOptions? baseOptions,
  bool isDebug = false,
  bool useSystemPrint = false,
  String? printTag,
  CheckNetWork? baseCheckNet,
  List<String>? ignoreCacheKeys,
  RequestCaptureError? requestCaptureError,
  CacheMode? baseCacheMode,
  HiveCipher? encryptionCipher,
  Map<String, dynamic>? baseUrlEnv,
  int cacheInvalidationTime = 365 * 24 * 60 * 60 * 1000,
  double debugWindowWidth = 600,
  double debugWindowHeight = 500
}) async{

  WidgetsFlutterBinding.ensureInitialized();
  LogUtil.init(isDebug: isDebug, tag: printTag, useSystemPrint: useSystemPrint);

  this._baseCheckNet = baseCheckNet;
  this._baseCacheMode = baseCacheMode;
  this._requestCaptureError = requestCaptureError;
  this._cacheInvalidationTime = cacheInvalidationTime;
  this._baseIgnoreCacheKeys = ignoreCacheKeys;

  if (baseOptions != null) {
    _client?.options = baseOptions;
  }
  /// 以最初 baseUrl 为准
  _client?.options.baseUrl = baseUrl;
  if (interceptors != null) {
    _client?.interceptors.addAll(interceptors);
  }
  if(baseUrlEnv!= null && baseUrlEnv.isNotEmpty){
    _baseUrlEnv.addAll(baseUrlEnv);
  }
  if(RxNetPlatform.isWeb){
    this._baseCacheMode = CacheMode.onlyRequest;
    LogUtil.v("RxNet 不支持缓存的环境:web");
    return;
  }
  _dataBase = RxNetDataBase();
  await RxNetDataBase.initDatabase(
      directory: cacheDir,
      hiveBoxName: cacheName,
      encryptionCipher: encryptionCipher);

  debugWindowSizeNotifier = ValueNotifier(Size(debugWindowWidth, debugWindowHeight));
}