skip to Main Content

I have created a Flutter Cubit class that has a method that create an object of itself, the problem is when I try to create a variable of this class with this method I get a type error, so what is the solution?

class AppCubit extends Cubit<AppStates>{
  AppCubit ():super(AppIntialState());

  static AppCubit get(context) => BlocProvider.of(context);

I tried to call this method with context, but I get the type error.

AppCubit cubit= AppCubit.get(context);

2

Answers


  1. Probably you need to provide the generic type, try changing this

    static AppCubit get(context) => BlocProvider.of(context);
    

    to this

    static AppCubit get(context) => BlocProvider.of<AppCubit>(context);
    
    Login or Signup to reply.
  2. Try like this:

      static AppCubit get(BuildContext context) =>
          BlocProvider.of<AppCubit>(context);
    

    You need to use BuildContext context inside get() or simply

      static AppCubit get(BuildContext _) => BlocProvider.of<AppCubit>(_);
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search