I'm trying to navigate to another page using a named route:
... RaisedButton( child: Text('Get image'), onPressed: () => Navigator.pushNamed<String>( context, '/second'),...
but get the error
> I/flutter (12439): ══╡ EXCEPTION CAUGHT BY GESTURE > ╞═══════════════════════════════════════════════════════════════════ > I/flutter (12439): The following assertion was thrown while handling a gesture: > I/flutter (12439): type 'MaterialPageRoute<dynamic>' is not a subtype of type 'Route<String>' > I/flutter (12439): Either the assertion indicates an error in the framework itself, or we should provide substantially > I/flutter (12439): more information in this error message to help you determine and fix the underlying cause. > I/flutter (12439): In either case, please report this assertion by filing a bug on GitHub: > I/flutter (12439): https://github.com/flutter/flutter/issues/new?template=BUG.md > I/flutter (12439): When the exception was thrown, this was the stack: > I/flutter (12439): #0 NavigatorState._routeNamed package:flutter/…/widgets/navigator.dart:1426 > I/flutter (12439): #1 NavigatorState.pushNamed package:flutter/…/widgets/navigator.dart:1473 > I/flutter (12439): #2 Navigator.pushNamed package:flutter/…/widgets/navigator.dart:740 > I/flutter (12439): #3 _CompleterState.build.<anonymous closure> package:my_package/completer.dart:205
The route is defined in the homepage:
class MyApp extends StatelessWidget { // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( title: 'XXXX', theme: ThemeData( primarySwatch: Colors.blue, ), home: Completer(), routes: { '/second': (BuildContext context) => SecondPage(), }, onUnknownRoute: (RouteSettings settings) { return MaterialPageRoute( builder: (BuildContext context) => Completer(), ); }, ); } }
I couldn't find anything on this error message, so I seem to have generated an error that no-one else has seen before.
I'm slowly hacking away at the code to simplify it to the bare minimum, to work out what I've done wrong, but any pointers would be appreciated.
A modal route that replaces the entire screen with a platform-adaptive transition. For Android, the entrance transition for the page zooms in and fades in while the exiting page zooms out and fades out. The exit transition is similar, but in reverse.
onGenerateRoute. The route generator callback used when the app is navigated to a named route. If this returns null when building the routes to handle the specified initialRoute, then all the routes are discarded and Navigator. defaultRouteName is used instead ( / ). See initialRoute.
In Flutter these elements are called routes and they're managed by a Navigator widget. The navigator manages a stack of Route objects and provides two ways for managing the stack, the declarative API Navigator. pages or imperative API Navigator. push and Navigator.
Blindingly obvious in hindsight.
I had added the String type-hint in Navigator.pushNamed<String>()
as I thought I could receive a typed value back from the call. Removing that and receiving it into a var solved the problem.
Leaving this up for anyone else who gets themselves into the same state.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With