skip to Main Content

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


  1. Chosen as BEST ANSWER

    In the end, this works for me

    To pass cubit and state to another class, you have to declare the class like this

    class YourClassName<T extends Cubit<S>, S> {
      const YourClassName();
    }
    

    To pass cubit and state to function, you have to declare fucntion like this

    void YourFunctionName<T extends Cubit<S>, S> {
      // your code here
    }
    

  2. abstract class BaseCubit<S> extends Cubit<S> should be abstract class BaseCubit extends Cubit<BaseState>.
    Inside constructor of LoginCubit you will pass : super(LoginState). Same with LogoutCubit.

    I think you should use BlocConsumer<T, BaseState> to access cubit inside GenericDialog.
    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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search