Home > Net >  Using two Repositories in one Cubit - Flutter BLoC
Using two Repositories in one Cubit - Flutter BLoC

Time:06-10

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.

CodePudding user response:

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...

  • Related