Search code examples
fluttermaterialpageroute

The argument type 'Route Function(RouteSettings)' can't be assigned to the parameter type 'List<Route> Function(String)?


class RoutesGenerator {

  static Route<dynamic> generateRoutes( RouteSettings settings) {
    switch(settings.name!) {
      case '/':
        return MaterialPageRoute(builder: (_) => const HomePage());
      case '/add_employee':
        return MaterialPageRoute(builder: (_) => const AddEmployee());
      default:
        return _errorRoute();
    }

  }
  static Route<dynamic> _errorRoute(){
    return MaterialPageRoute(builder: (_) {
      return MaterialApp(
        home: Scaffold(
          appBar: AppBar(
            title: const Text("No page found"),
          ),
          body: const Center(
            child: Text('No route is exists.',
            style: TextStyle(
              color: Colors.red,
              fontWeight: FontWeight.bold,
              fontSize: 20.0,
            ),),
          ),
        ),
      );
    });
  }

The above code gives an error. Error message:

The argument type 'Route Function(RouteSettings)' can't be assigned to the parameter type 'List<Route> Function(String)?


Solution

  • You don't need to use multiple MaterialApp, you can remove it from _errorRoute.

    And on your app widget MaterialApp use like onGenerateRoute: RoutesGenerator.generateRoutes,

    class MyApp extends StatelessWidget {
      const MyApp({Key? key}) : super(key: key);
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          onGenerateRoute: RoutesGenerator.generateRoutes,