1

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);
Benjamin Buch
  • 4,752
  • 7
  • 28
  • 51
John Samir
  • 11
  • 3

2 Answers2

0

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);
Almis
  • 3,684
  • 2
  • 28
  • 58
0

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>(_);

Soliev
  • 1,180
  • 1
  • 1
  • 12