I have the below generic Bloc which I am trying to use.

class ManageFeedbackBloc<T extends IContribute>
extends Bloc<ManageFeedbackEvent, ManageFeedbackState> {}

I used the below provider to create it:

void _navigateToContributionPage(
  BuildContext context, FeedbackCategory category) {
final userFeedbackBloc = BlocProvider.of<UserFeedbacksBloc>(context);
final manageFeedbackBloc = ManageFeedbackBloc<DoctorFeedback>(
    repository: ManageFeedbackRepository());

final manageFeedbackDestination =
    BlocProvider<ManageFeedbackBloc<DoctorFeedback>>(
  create: (context) => manageFeedbackBloc,
  child: ManageFeedbackPage<DoctorFeedback>(),
  lazy: false,
);

userFeedbackBloc.monitorFeedbackChanges(
    BlocProvider.of<ManageFeedbackBloc<DoctorFeedback>>(context));

Navigator.of(context).pushReplacement(
    MaterialPageRoute(builder: (context) => manageFeedbackDestination));

}

The above code crashes on this line:

userFeedbackBloc.monitorFeedbackChanges(
    BlocProvider.of<ManageFeedbackBloc<DoctorFeedback>>(context));

I get the error:

Another exception was thrown: Error: Could not find the correct Provider<ManageFeedbackBloc> above this ContributionSelectorPage Widget

If I also try to skip the above and use BlocBuilder, I get the same error. Please see the code below:

 class ManageFeedbackPage<T extends IContribute> extends StatelessWidget {
  final _formKey = GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {
    return BlocConsumer<ManageFeedbackBloc<T>, ManageFeedbackState>(
      listenWhen: (_, currentState) => currentState.isListenerState,
      buildWhen: (_, currentState) => !currentState.isListenerState,
      listener: (context, state) async {
        if (state is ManageFeedbackPromptUpdate) {
          _promptActionAlert(
              context,
              'Modification',
              'Êtes vous sûr de vouloir modifier ce retour d\'expérience ?',
              ManageFeedbackUpdateFeedback());
          return;
        }
        throw UnimplementedError(
            'manage_feedback_page - UnImplemented State: Tried to implement listening state to ${state.toString()}');
      },
      builder: (context, state) {
        if (state is ManageFeedbackLoading) {
          return _getLoadingState();
        }

        return _getLoadedState(context);
      },
    );
  }
 }

Is it that generic bloc is not supported?

I am using flutter_bloc: ^7.0.0


Solution 1: Maz

So I believe I found the problem:

final manageFeedbackDestination =
    BlocProvider<ManageFeedbackBloc<DoctorFeedback>>(
  create: (context) => manageFeedbackBloc,
  child: ManageFeedbackPage<DoctorFeedback>(),
  lazy: false,
);

BlocProvider type should not specify the generic type just the type of the BloC without the generic.

final manageFeedbackDestination =
    BlocProvider<ManageFeedbackBloc>(
  create: (context) => manageFeedbackBloc,
  child: ManageFeedbackPage<DoctorFeedback>(),
  lazy: false,
);

This worked for me BlocProvider<ManageFeedbackBloc> instead of BlocProvider<ManageFeedbackBloc<DoctorFeedback>>.