在一个肘中使用两个存储库-Flutter Bloc

发布于 2025-02-06 08:01:29 字数 566 浏览 2 评论 0原文

我想知道我是否可以在一个cubit 中实例化两个存储库?像这样:

class LoginCubit extends Cubit<LoginState> {  
LoginCubit(this._authRepository, this._authenticationRepository) 
     : super(const LoginState());  

final FirebaseAuthRepository _authenticationRepository;  

final AuthRepository _authRepository;

我想使用我的Cubit时该怎么办:

BlocProvider(create: (_) => LoginCubit(context.read<AuthRepository, FirebaseAuthRepository>()),

我会遇到此linter错误:

- &gt; 2位置论点预期,但发现1个。尝试添加丢失的参数。

I was wondering if I could instantiate two Repositories in one Cubit ? Like so :

class LoginCubit extends Cubit<LoginState> {  
LoginCubit(this._authRepository, this._authenticationRepository) 
     : super(const LoginState());  

final FirebaseAuthRepository _authenticationRepository;  

final AuthRepository _authRepository;

I get stuck on what to do when I'd like to use my cubit :

BlocProvider(create: (_) => LoginCubit(context.read<AuthRepository, FirebaseAuthRepository>()),

I get this linter error :

-> 2 positional argument(s) expected, but 1 found. Try adding the missing arguments.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柒七 2025-02-13 08:01:30

在CUBIT中拥有多个存储库绝对没有问题。您的构造函数是问题所在。您需要将两个变量传递给Cubits构造函数。 IE:

LoginCubit(firstParameter, secondParameter)

那么您如何获得这些参数可能会有所不同...

您可以执行EG:

LoginCubit(AuthRepository(), FirebaseAuthRepository())

或:

LoginCubit(context.read<AuthRepository>(), context.read<FirebaseAuthRepository>()),

或者您获得两个存储库...

我是否建议您查看名为Getit的服务定位器软件包以及名为注射的软件包。那这些事情将为您解决...

Absolutely no problem to have multiple repositories in a Cubit. Your constructor call is the problem. You need to pass two variables to the cubits constructor. I.e.:

LoginCubit(firstParameter, secondParameter)

Then how you get those parameters can differ...

You could do e.g.:

LoginCubit(AuthRepository(), FirebaseAuthRepository())

or:

LoginCubit(context.read<AuthRepository>(), context.read<FirebaseAuthRepository>()),

or however you get your two repositories...

May I suggest you to look into the service locator package named getIt together with the package named injectable. Then those things will be solved for you...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文