Home > Blockchain >  type '(BuildContext, Widget) => ChangeNotifierProvider<>' is not a subtype of typ
type '(BuildContext, Widget) => ChangeNotifierProvider<>' is not a subtype of typ

Time:10-21

type '(BuildContext, Widget) => ChangeNotifierProvider' is not a subtype of type '(BuildContext, Widget?) => Widget' in type cast

Hi, I am facing above error on my old project while I was converting it to Flutter 2.5, can anyone help me to figure it out?

abstract class Role {
  Widget homeBuilder(BuildContext context);
   Map<String, WidgetBuilder>? routes;

  /// The navigatorBuilder allows you to insert widgets between MaterialApp and its Navigator.
  /// This is useful for providing the logged in Talent or Recruiter
   TransitionBuilder? navigatorBuilder;
}




@override
  TransitionBuilder navigatorBuilder =
      (BuildContext context, Widget navigator) {
    String token = Provider.of<AuthBloc>(context).token;
    MyNotifications.setUpFirebaseMessaging(context);
    return ChangeNotifierProvider<Talent>(
      create: (ctx) =>
      (Provider.of<AuthBloc>(context).role as TalentRole).talent
        ..setAuthToken(token),
      child: navigator,
    );
  };

CodePudding user response:

TransitionBuilder:

typedef TransitionBuilder = Widget Function(BuildContext context, Widget? child);

expects a context and a child. The child is of type Widget?, note the question mark. This shows you the widget can be null.

If you define a TransitionBuilder you need to show that too, so you need to change

@override
  TransitionBuilder navigatorBuilder =
      (BuildContext context, Widget navigator) {

to

@override
  TransitionBuilder navigatorBuilder =
      (BuildContext context, Widget? navigator) { //add the question mark

More info on null safety: https://dart.dev/null-safety/understanding-null-safety

  • Related