Skip to content

type 'MaterialPageRoute<dynamic>' is not a subtype of type 'Route<String?>?' of ' in type cast' #57186

Closed

Description

@0x1af2aec8f957

Hi, I found an uncertain problem!

Problem description:

I expect to use Navigator.of(context).pop<String> to return a value of a certain type, but the result is always a value of dynamic type.

Code:

// First screen
Navigator.of(context).pushNamed<String>('login').then((String result){
                                  print('login.result runtimeType: ${result.runtimeType}');
                                });

// Second screen
Navigator.of(context).pop<String>('success');

A type error will occur in the above code:

E/flutter (16614): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: type 'MaterialPageRoute<dynamic>' is not a subtype of type 'Route<String>' in type cast
E/flutter (16614): #0      NavigatorState._routeNamed (package:flutter/src/widgets/navigator.dart:3138:55)
E/flutter (16614): #1      NavigatorState.pushNamed (package:flutter/src/widgets/navigator.dart:3190:20)
E/flutter (16614): #2      _Transfer.build.<anonymous closure> (package:example/views/FirstScreen.dart:157:55)
E/flutter (16614): #3      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24)
E/flutter (16614): #4      TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:504:11)
E/flutter (16614): #5      BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:282:5)
E/flutter (16614): #6      BaseTapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:254:7)
E/flutter (16614): #7      GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:156:27)
E/flutter (16614): #8      GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:222:20)
E/flutter (16614): #9      GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:198:22)
E/flutter (16614): #10     GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:156:7)
E/flutter (16614): #11     GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:102:7)
E/flutter (16614): #12     GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:86:7)
E/flutter (16614): #13     _rootRunUnary (dart:async/zone.dart:1196:13)
E/flutter (16614): #14     _CustomZone.runUnary (dart:async/zone.dart:1085:19)
E/flutter (16614): #15     _CustomZone.runUnaryGuarded (dart:async/zone.dart:987:7)
E/flutter (16614): #16     _invoke1 (dart:ui/hooks.dart:275:10)
E/flutter (16614): #17     _dispatchPointerDataPacket (dart:ui/hooks.dart:184:5)
E/flutter (16614): 

Activity

  1. liodali commented on May 14, 2020

    @liodali

    you don't need to make pushNamed() or pop()
    just pushNamed("routeName") and pop(value)
    you will get right type

  2. VladyslavBondarenko commented on May 14, 2020

    @VladyslavBondarenko

    Thanks @liodali ,
    It should be solved by this, closing.
    @notescript If you disagree please write in the comments and I will reopen it

    https://flutter.dev/docs/cookbook/navigation/returning-data#interactive-example

  3. HannibalKcc commented on Jun 15, 2020

    @HannibalKcc

    Hi @VladyslavBondarenko ,
    your example used Navigator.push(), return data's type work well.

    But, i find if i use Navigator.pushNamed() replace Navigator.push() it throw A type error will occur as same as @notescript showed.

    you can check code
    here

  4. liodali commented on Jun 15, 2020

    @liodali

    replace String result by var result,should work for you

  5. HannibalKcc commented on Jun 16, 2020

    @HannibalKcc

    @liodali use var work for me.

    But i think API Navigator.pushNamed() and Navigator.push() should have the same result,even certain a type.

  6. changed the title [-]Navigator.of(context).pushNamed<T> cannot determine the type of T[/-] [+]Unhandled Exception: type 'MaterialPageRoute<dynamic>' is not a subtype of type 'Route<String>' in type cast[/+] on Jun 16, 2020
  7. added
    p: material_uimaterial_ui package in flutter/packages
    f: routesNavigator, Router, and related APIs.
    frameworkflutter/packages/flutter repository. See also f: labels.
    has reproducible stepsThe issue has been confirmed reproducible and is ready to work on
    on Jun 16, 2020
  8. celesteking commented on Nov 17, 2020

    @celesteking

    This is a deficiency on flutter's Navigator1/MaterialApp side when routes table is being used. This isn't documented anywhere, but it should've been. The workaround is to stop providing pushNamed any type, but instead typecast to it later:

     final params = (await Navigator.of(context).pushNamed('/xx', arguments: RouteArgs('abc'))) as RouteRetParams;
    // params  is now of `RouteRetParams` type

    This needs fixing, either on doc or code 's side.

  9. chunhtai commented on Oct 10, 2022

    @chunhtai
    Contributor

    This is also blocking internal migration

  10. 6 remaining items

  11. flutter-triage-bot commented on Jan 12, 2024

    @flutter-triage-bot

    This issue is missing a priority label. Please set a priority label when adding the triaged-design label.

  12. added
    P2Important issues not at the top of the work list
    triaged-designTriaged by Design Languages team
    on Jan 17, 2024
  13. equescodebelike commented on Aug 14, 2025

    @equescodebelike

    I had similar problem, but with auto_route navigation, so I fixed cast issue by adding <type> to CustomRoute declaration:

        CustomRoute<int?>(
          name: RoutesNames.imageSliderFullScreen,
          path: Routes.imageSliderFullScreen,
          page: ImageSliderWithExtendedImageFullScreen,
          customRouteBuilder: dismissablePageRouteBuilder,
        ),
    
  14. added
    packageflutter/packages repository. See also p: labels.
    and removed
    frameworkflutter/packages/flutter repository. See also f: labels.
    on Aug 17, 2026
  15. added a commit that references this issue on Aug 27, 2026
    c18aa80
  16. github-actions commented on Sep 10, 2026

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  17. locked as resolved and limited conversation to collaborators on Sep 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work listf: routesNavigator, Router, and related APIs.found in release: 3.3Found to occur in 3.3found in release: 3.7Found to occur in 3.7has reproducible stepsThe issue has been confirmed reproducible and is ready to work onp: material_uimaterial_ui package in flutter/packagespackageflutter/packages repository. See also p: labels.team-designOwned by Design Languages teamtriaged-designTriaged by Design Languages teamworkaround availableThere is a workaround available to overcome the issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions