flutterflutter-routes

Routing in flutter doesn't work correctly


Routes not functioning correctly in Flutter

This is my routes.dart code:

import 'package:flutter/material.dart';
import 'package:gowild/Screens/homeScreen.dart';
import '../screens/login_screen.dart';
import '../screens/registration_screen.dart';
class AppRoutes {
    static const String home = '/';
    static const String registration = '/registration';
    static const String homeScreen = '/homeScreen';
    static final Map<String, WidgetBuilder> routes = {
    AppRoutes.home: (context) => const RegistrationScreen(),
    AppRoutes.homeScreen: (context) => const HomeScreen(),
    AppRoutes.registration: (context) => const RegistrationScreen(),
    };
}

When the login button is clicked, I called pushNamed as shown below:

onSubmitAnimationCompleted: () { Navigator.pushNamed(context, AppRoutes.homeScreen); }

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Could not find a generator for route RouteSettings("/homefeed", null) in the _WidgetsAppState.E/flutter ( 4592): Make sure your root app widget has provided a way to generateE/flutter ( 4592): this route.E/flutter ( 4592): Generators for routes are searched for in the following order:E/flutter ( 4592):  1. For the "/" route, the "home" property, if non-null, is used.E/flutter ( 4592):  2. Otherwise, the "routes" table is used, if it has an entry for the route.E/flutter ( 4592):  3. Otherwise, onGenerateRoute is called. It should return a non-null value for any valid route not handled by "home" and "routes".E/flutter ( 4592):  4. Finally if all else fails onUnknownRoute is called.E/flutter ( 4592): Unfortunately, onUnknownRoute was not set.

This happens for every route I assigned except the home route. All other routes are not working and give the above error.

I tried both types, calling AppRoutes.homeScreen and '/homeScreen', but nothing works.

here is my main.dart

import 'package:flutter/material.dart';
import 'login_screen.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    final theme = ThemeData(
      unselectedWidgetColor: const Color.fromARGB(255, 165, 160, 160),
      // brightness: Brightness.dark,
      textTheme: const TextTheme(
        displaySmall: TextStyle(
          fontFamily: 'OpenSans',
          fontSize: 45.0,
          color: Color.fromARGB(255, 255, 255, 255),
        ),
        labelLarge: TextStyle(
          fontFamily: 'OpenSans',
          fontWeight: FontWeight.bold,
          fontStyle: FontStyle.italic,
          color: Colors.white,
        ),
        titleMedium: TextStyle(
          fontFamily: 'NotoSans',
          color: Colors.white,
        ),
        bodyMedium: TextStyle(
          fontFamily: 'NotoSans',
          color: Colors.white,
        ),
      ),
      colorScheme: ColorScheme.fromSwatch()
          // .copyWith(brightness: )
          .copyWith(secondary: Colors.orange)
          .copyWith(
              outline: const Color.fromARGB(255, 13, 158, 61)) //appbar color
          .copyWith(
              background: const Color.fromARGB(
                  255, 39, 38, 38)) //background color dont change
          .copyWith(
              primary: const Color.fromARGB(255, 13, 158, 61)) //primary color
          .copyWith(
              onPrimary: const Color.fromARGB(255, 254, 255, 255)) //text color
          .copyWith(onSecondary: const Color.fromARGB(255, 15, 119, 55)),
    );

    return MaterialApp(
      title: 'GoWild',
      theme: theme,
      home: const LoginScreen(),
    );
  }
}

Solution

  • Your materialApp needs to have routes or onGenerator route input to recognize named routes.

    return MaterialApp(
      title: 'GoWild',
      theme: theme,
      home: const LoginScreen(),
      routes: <String, WidgetBuilder>{
        AppRoutes.home: (context) => const RegistrationScreen(),
        AppRoutes.homeScreen: (context) => const HomeScreen(),
        AppRoutes.registration: (context) => const RegistrationScreen(),
      },
    );