I am using Flutter 3.3.7 Stable release and writing a simple application with everything working perfectly but I got errors on named routes...
Please note that I only need named routes with limited codes not direct class calling etc...
Well, my main.dart
is as follows...
class _MyAppState extends State<MyApp> {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Run With',
theme: ThemeData(
primarySwatch: Colors.blue,
),
navigatorKey: TopVariables.appNavigationKey,
initialRoute: '/',
routes: {
'/': (context) => const Home(),
'/SignIn': (context) => SignIn(),
'/SignUp': (context) => SignUp(),
},
);
}
}
and I am calling the route as...
GestureDetector(
onTap: () {
Navigator.pushReplacementNamed(context, "/SignUp");
},
child: Text(
"SignUp",
),
)
But getting the below error...
======== Exception caught by gesture ===============================================================
The following assertion was thrown while handling a gesture:
Could not find a generator for route RouteSettings("/SignUp", null) in the _WidgetsAppState.
Make sure your root app widget has provided a way to generate
this route.
Generators for routes are searched for in the following order:
1. For the "/" route, the "home" property, if non-null, is used.
2. Otherwise, the "routes" table is used, if it has an entry for the route.
3. Otherwise, onGenerateRoute is called. It should return a non-null value for any valid route not handled by "home" and "routes".
4. Finally if all else fails onUnknownRoute is called.
Unfortunately, onUnknownRoute was not set.
When the exception was thrown, this was the stack:
#0 _WidgetsAppState._onUnknownRoute.<anonymous closure> (package:flutter/src/widgets/app.dart:1421:9)
#1 _WidgetsAppState._onUnknownRoute (package:flutter/src/widgets/app.dart:1436:6)
#2 NavigatorState._routeNamed (package:flutter/src/widgets/navigator.dart:4092:37)
#3 NavigatorState.pushReplacementNamed (package:flutter/src/widgets/navigator.dart:4206:35)
#4 Navigator.pushReplacementNamed (package:flutter/src/widgets/navigator.dart:1746:34)
#5 _SignInState.build.<anonymous closure> (package:runwith/screens/signin.dart:252:37)
#6 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:253:24)
#7 TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:627:11)
#8 BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:306:5)
#9 BaseTapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:239:7)
#10 PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:615:9)
#11 PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:98:12)
#12 PointerRouter._dispatchEventToRoutes.<anonymous closure> (package:flutter/src/gestures/pointer_router.dart:143:9)
#13 _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:617:13)
#14 PointerRouter._dispatchEventToRoutes (package:flutter/src/gestures/pointer_router.dart:141:18)
#15 PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:127:7)
#16 GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:460:19)
#17 GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:440:22)
#18 RendererBinding.dispatchEvent (package:flutter/src/rendering/binding.dart:337:11)
#19 GestureBinding._handlePointerEventImmediately (package:flutter/src/gestures/binding.dart:395:7)
#20 GestureBinding.handlePointerEvent (package:flutter/src/gestures/binding.dart:357:5)
#21 GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:314:7)
#22 GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:295:7)
#23 _invoke1 (dart:ui/hooks.dart:167:13)
#24 PlatformDispatcher._dispatchPointerDataPacket (dart:ui/platform_dispatcher.dart:341:7)
#25 _dispatchPointerDataPacket (dart:ui/hooks.dart:94:31)
Handler: "onTap"
Recognizer: TapGestureRecognizer#3e320
debugOwner: GestureDetector
state: possible
won arena
finalPosition: Offset(254.7, 717.9)
finalLocalPosition: Offset(128.0, 11.7)
button: 1
sent tap down
====================================================================================================
My flutter doctor
result is...
[✓] Flutter (Channel stable, 3.3.7, on macOS 12.6 21G115 darwin-arm, locale en-PK)
• Flutter version 3.3.7 on channel stable at /Users/exeideas/AndroidStudioProjects/flutter_v3.3.7
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision e99c9c7cd9 (8 days ago), 2022-11-01 16:59:00 -0700
• Engine revision 857bd6b74c
• Dart version 2.18.4
• DevTools version 2.15.0
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
• Android SDK at /Users/exeideas/Library/Android/sdk
• Platform android-33, build-tools 33.0.0
• Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS (Xcode 14.1)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Build 14B47b
• CocoaPods version 1.11.3
[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
[✓] Android Studio (version 2021.3)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)
[✓] Connected device (3 available)
• SM A325F (mobile) • RF8R509QMYF • android-arm64 • Android 12 (API 31)
• macOS (desktop) • macos • darwin-arm64 • macOS 12.6 21G115 darwin-arm
• Chrome (web) • chrome • web-javascript • Google Chrome 107.0.5304.110
[✓] HTTP Host Availability
• All required HTTP hosts are available
• No issues found!
Process finished with exit code 0
I simulate your behaviour and everything works as expected on Flutter 3.3.7
My code example
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Run With',
theme: ThemeData(
primarySwatch: Colors.blue,
),
initialRoute: '/',
routes: {
'/': (context) => SafeArea(
child: Scaffold(
body: Center(child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text('Home'),
ElevatedButton(onPressed: () {
Navigator.pushReplacementNamed(context, "/SignUp");
}, child: Text('To SignUp'))
],
),),
),
),
'/SignIn': (context) => const SafeArea(child: Scaffold(body:Center(child: Text('SignIn'),))),
'/SignUp': (context) => const SafeArea(child: Scaffold(body: Center(child: Text('SignUp'),))),
},
);
}
}