Joel
Joel

Reputation: 210

A value of type 'Null' can't be returned from the method 'onGenerateRoute' because it has a return type of 'Route<dynamic>'

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;
    }
  }
}

Upvotes: 2

Views: 1554

Answers (3)

Mahdi Gharooni
Mahdi Gharooni

Reputation: 62

I'm sure you shouldn't return null, you must return a MaterialPageRoute in every state. You can define a default page like an empty container or an error page to display in unknown routeSettings.

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

I hope it could help you.

Upvotes: 1

Arti Scream
Arti Scream

Reputation: 140

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.

Upvotes: 0

Joel
Joel

Reputation: 210

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;
    }
  }
}

Upvotes: 3

Related Questions