Home > OS >  A value of type 'Null' can't be returned from the method 'onGenerateRoute'
A value of type 'Null' can't be returned from the method 'onGenerateRoute'

Time:12-27

Actually I was trying to add genearatedRoute but i was facing this error A value of type 'Null' can't be returned from the method 'onGenerateRoute' because it has a return type of 'Route' Below is my code

class AppRouter {
   Route onGenerateRoute(RouteSettings routeSettings) {
    switch (routeSettings.name) {
      case '/':
        return MaterialPageRoute(builder: (_) => const SplashScreen());
        break;
      default:
        return null;
    }
  }
}

CodePudding user response:

Below code fixed the issue for me

class AppRouter {
  Route? onGenerateRoute(RouteSettings routeSettings) {
    switch (routeSettings.name) {
      case '/':
        return MaterialPageRoute(builder: (_) => const SplashScreen());
        break;
      default:
        return null;
    }
  }
}

CodePudding user response:

It is because you have null returning in default, and you must return Route object from function. To properly fix it, maybe in default option specify route to home screen.

  • Related