Flutter Provider - 使用 ProxyProvider 的循环依赖
Flutter Provider - Circular Dependencies using ProxyProvider`
我有以下服务:
SecuredStorageService()
ApiService({this.authService})
AuthService({this.securedStorageService, this.apiService})
RegisterService({this.apiService, this.securedStorageService})
这让我写:
providers: [
Provider<SecuredStorageService>.value(value: SecuredStorageService()),
ProxyProvider<AuthService, ApiService>(
builder: (_, auth, __) => ApiService(authService: auth),
),
ProxyProvider2<ApiService, SecuredStorageService, RegisterService>(
builder: (_, api, storage, __) => RegisterService(apiService: api, securedStorageService: storage),
),
ProxyProvider2<ApiService, SecuredStorageService, AuthService>(
builder: (_, api, storage, __) => AuthService(apiService: api, securedStorageService: storage),
),
],
到目前为止我可以看出它已经看起来很乱了。但事实并非如此。当我 运行 应用程序时,出现以下错误:
那我怎么办?我在所有 ProxyProvider
之前添加一个 Provider<AuthService>
。但是,AuthService 被构造了两次!这失去了作为一个单一实例的全部意义(或者不是吗?)。
我的主要目标是像 Angular 或 Laravel.
那样进行某种依赖注入
ProxyProvider
和小部件通常与循环依赖作斗争,因为它通常是 "spaghetti code" (see more)
的标志
因此,使用 ProxyProvider
您将无法制作循环依赖图。
如果这确实是您想要的,请考虑使用 Provider.value
并手动处理您的依赖项。
我有以下服务:
SecuredStorageService()
ApiService({this.authService})
AuthService({this.securedStorageService, this.apiService})
RegisterService({this.apiService, this.securedStorageService})
这让我写:
providers: [
Provider<SecuredStorageService>.value(value: SecuredStorageService()),
ProxyProvider<AuthService, ApiService>(
builder: (_, auth, __) => ApiService(authService: auth),
),
ProxyProvider2<ApiService, SecuredStorageService, RegisterService>(
builder: (_, api, storage, __) => RegisterService(apiService: api, securedStorageService: storage),
),
ProxyProvider2<ApiService, SecuredStorageService, AuthService>(
builder: (_, api, storage, __) => AuthService(apiService: api, securedStorageService: storage),
),
],
到目前为止我可以看出它已经看起来很乱了。但事实并非如此。当我 运行 应用程序时,出现以下错误:
那我怎么办?我在所有 ProxyProvider
之前添加一个 Provider<AuthService>
。但是,AuthService 被构造了两次!这失去了作为一个单一实例的全部意义(或者不是吗?)。
我的主要目标是像 Angular 或 Laravel.
那样进行某种依赖注入ProxyProvider
和小部件通常与循环依赖作斗争,因为它通常是 "spaghetti code" (see more)
因此,使用 ProxyProvider
您将无法制作循环依赖图。
如果这确实是您想要的,请考虑使用 Provider.value
并手动处理您的依赖项。