Eli*_*hen 4 circular-dependency dart flutter flutter-provider
我有以下服务:
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),
),
],
Run Code Online (Sandbox Code Playgroud)
到目前为止,我可以说它看起来已经很凌乱了。但事实并非如此。当我运行该应用程序时,出现以下错误:
那我怎么办?我在所有ProxyProvidersa之前添加Provider<AuthService>。但是,AuthService 被构造了两次!这失去了作为单一实例的全部意义(或者不是?)。
我的主要目标是进行某种依赖注入,就像在 Angular 或 Laravel 中一样。
| 归档时间: |
|
| 查看次数: |
1459 次 |
| 最近记录: |