I have a generic dialog that I use for everything, such as login and logout. The constructor looks like this
GenericDialog({
required String dialogTitle,
required Widget dialogContent,
});
Now I’m trying to add BlocConsumer into the dialog, in order for it to work, I need to pass Cubit and State into the constructor. Below are the cubit and state I had prepared
abstract class BaseState extends Equatable
class LoginState extends BaseState
class LogoutState extends BaseState
abstract class BaseCubit<S> extends Cubit<S>
class LoginCubit extends BaseCubit<LoginState>
class LogoutCubit extends BaseCubit<LogoutState>
Below is the constructor
GenericDialog({
required String dialogTitle,
required Widget dialogContent,
required BaseCubit cubit,
required BaseState state,
});
how i use it
GenericDialog({
dialogTitle: 'LOGIN',
dialogContent: widget,
cubit: LoginCubit,
state: LoginState,
});
When i pass the cubit and state into the constructor, i get
The argument type 'Type' can't be assigned to the parameter type 'BaseCubit<dynamic>'
2
Answers
In the end, this works for me
To pass cubit and state to another class, you have to declare the class like this
To pass cubit and state to function, you have to declare fucntion like this
abstract class BaseCubit<S> extends Cubit<S>
should beabstract class BaseCubit extends Cubit<BaseState>
.Inside constructor of
LoginCubit
you will pass: super(LoginState)
. Same withLogoutCubit
.I think you should use
BlocConsumer<T, BaseState>
to access cubit insideGenericDialog
.And dialog would be
GenericDialog<T extends BaseCubit> extends ... {}
Then outside dialog you should create your cubit using
BlocProvider(...)
.You will call
GenericDialog<LoginCubit>({ dialogTitle: 'LOGIN', dialogContent: widget })
.Source
I did something like that with my own project