Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Named route navigation error: type 'MaterialPageRoute<dynamic>' is not a subtype of type 'Route<String>'

Tags:

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.

like image 525
Michael Davies Avatar asked Feb 28 '19 17:02

Michael Davies


People also ask

What is Materialpageroute in Flutter?

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.

What is onGenerateRoute in Flutter?

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.

What is the use of Navigator in Flutter?

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.


1 Answers

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.

like image 62
Michael Davies Avatar answered Sep 17 '22 03:09

Michael Davies