initNet method
Implementation
Future<void> initNet({required RxNetConfig config}) async {
LogUtil.init(systemLog: config.systemLog,debug: config.isDebug);
this._rxNetConfig = config;
debugWindow = ValueNotifier(Size(config.debugWindowWidth, config.debugWindowHeight));
// 如果提供了自定义适配器,使用它;否则使用默认的 DioAdapter
// If custom adapter provided, use it; otherwise use default DioAdapter
if (adapter != null) {
_adapter = adapter;
} else if (_adapter == null) {
// 只在 _adapter 为 null 时创建默认适配器
// Only create default adapter when _adapter is null
_adapter = DioAdapter();
}
if (config.adapterBaseOptions != null) {
_adapter?.applyBaseOptions(config.adapterBaseOptions!);
}
// 设置 baseUrl(所有适配器统一处理)
_adapter?.setBaseUrl(baseUrl);
// 添加适配器拦截器(适用于所有适配器)
if (config.interceptors != null && config.interceptors!.isNotEmpty) {
for (var interceptor in config.interceptors!) {
_adapter?.addInterceptor(interceptor);
}
}
_database = RxNetDataBase();
// 立即绑定数据库引用,确保 init 期间的缓存操作也能通过 cacheManager 访问
cacheManager.setDatabase(_database!);
cacheManager.setCacheInvalidationTime(config.cacheInvalidationTime);
cacheManager.setMaxCacheSize(config.cacheMaxSize);
cacheManager.setEvictionPolicy(config.cacheEvictionPolicy);
await _database!.init(
databasePath: config.cachePath,
databaseName: config.databaseName,
cacheName: config.cacheName,
);
}