BlocProvider.of() called with a context that does not contain a StateStreamableSource<Object?>

35 Views Asked by At

I'm trying to use Cubits to manage the states of only specific screen (Profile Screen)

and i have already provided my bloc and consumed it, but there's an unexpected exception occurred:

BlocProvider.of() called with a context that does not contain a StateStreamableSource<Object?>.

it's known, that the consumer start searching for the bloc provider in the higher levels of the widget tree (it's exist directly above the consumer).

here's the screen

  Widget build(BuildContext context) {
    return BlocProvider(
        create:(context)=>ProfileScreenCubit(),
        child: BlocConsumer<ProfileScreenCubit,ProfileScreenStates>(
        listener: (context,state){},
        builder: (context,state){
      
          var cubit = ProfileScreenCubit.getCubit(context); // exception here

          return Scaffold(...);
        },
      ),
    );
  }

here's the cubit

final class ProfileScreenCubit extends Cubit<ProfileScreenStates>{

  ProfileScreenCubit():super(ProfileScreenInitialState());

  static getCubit(BuildContext context)=>BlocProvider.of(context);


}

actually, there are other screens, that have been managed in that way. it's weird!

your help is appreciated, Thanks.

1

There are 1 best solutions below

0
A-E On BEST ANSWER

Ok, figured it out.

i'm returning dynamic

static getCubit(BuildContext context)=>BlocProvider.of(context);

moreover i'm defining my cubit as var

  var cubit = ProfileScreenCubit.getCubit(context); // exception here

it's depending on dart analyzer to infer the type (will always be dynamic).

hence, it's not a Streamable Source.

Fix

  static ProfileScreenCubit getCubit(BuildContext context)=>BlocProvider.of(context);

add the return data type of type ProfileScreenCubit