flutterroutesrequirednon-nullable

The parameter 'builder' can't have a value of 'null' because of its type, but the implicit default value is 'null'


I am trying to use my previous app with the following code, but it seems some updates have happened in Flutter and I get the following Error:

import 'package:flutter/material.dart';

class CustomRoute<T> extends MaterialPageRoute<T> {
  CustomRoute({
    WidgetBuilder builder,
    RouteSettings settings,
  }) : super(
          builder: builder,
          settings: settings,
        );

  @override
  Widget buildTransitions(
    BuildContext context,
    Animation<double> animation,
    Animation<double> secondaryAnimation,
    Widget child,
  ) {
    if (settings.name == '/') {
      return child;
    }
    return FadeTransition(
      opacity: animation,
      child: child,
    );
  }
}

class CustomPageTransitionBuilder extends PageTransitionsBuilder {
  @override
  Widget buildTransitions<T>(
    PageRoute<T> route,
    BuildContext context,
    Animation<double> animation,
    Animation<double> secondaryAnimation,
    Widget child,
  ) {
    if (route.settings.name == '/') {
      return child;
    }
    return FadeTransition(
      opacity: animation,
      child: child,
    );
  }
}

Error:

The parameter 'builder' can't have a value of 'null' because of its type, but the implicit default value is 'null'. Try adding either an explicit non-'null' default value or the 'required' modifier.

I tried to add either required or @required but neither worked.`


Solution

  • can you use like this? because it not gives error:

    import 'package:flutter/material.dart';
    
    class CustomRoute<T> extends MaterialPageRoute<T> {
      CustomRoute({
       required WidgetBuilder builder,
       required RouteSettings settings,
      }) : super(
              builder: builder,
              settings: settings,
            );
    
      @override
      Widget buildTransitions(
        BuildContext context,
        Animation<double> animation,
        Animation<double> secondaryAnimation,
        Widget child,
      ) {
        if (settings.name == '/') {
          return child;
        }
        return FadeTransition(
          opacity: animation,
          child: child,
        );
      }
    }
    
    class CustomPageTransitionBuilder extends PageTransitionsBuilder {
      @override
      Widget buildTransitions<T>(
        PageRoute<T> route,
        BuildContext context,
        Animation<double> animation,
        Animation<double> secondaryAnimation,
        Widget child,
      ) {
        if (route.settings.name == '/') {
          return child;
        }
        return FadeTransition(
          opacity: animation,
          child: child,
        );
      }
    }
    

    I tried to add required and it worked.