Skip to content

Improve error message for type mismatch in Navigator.pop and maybePop.#186571

Merged
auto-submit[bot] merged 8 commits into
flutter:masterfrom
puneetkukreja98:fix-16987
Jun 2, 2026
Merged

Improve error message for type mismatch in Navigator.pop and maybePop.#186571
auto-submit[bot] merged 8 commits into
flutter:masterfrom
puneetkukreja98:fix-16987

Conversation

@puneetkukreja98

Copy link
Copy Markdown
Contributor

Context
Currently, if a developer opens a strongly-typed route (such as showDialog<Null>) but accidentally pops it with an incompatible value (like Navigator.pop(context, "CANCEL")). Because the generic types do not match, the Dart runtime instantly throws a cryptic native TypeError (e.g., type 'String' is not a subtype of type 'Null') without any context about which route or widget caused it.

Changes

  • Added a _debugCheckCanConsumeResult helper to Route<T> that proactively validates result is T?.
  • Intercepts Navigator.pop, Navigator.maybePop, before they modify the stack or lock the Navigator.
  • If a type mismatch occurs, it now throws a highly descriptive FlutterError that pinpoints the exact expected type, the provided type, and the specific Route object involved.
  • Added comprehensive tests to navigator_test.dart to verify this behavior for pop and maybePop using a real-world showDialog scenario.

Fixes #16987

Pre-launch Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance.

Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the gemini-code-assist bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.

@flutter-dashboard flutter-dashboard Bot added the CICD Run CI/CD label May 15, 2026
@github-actions github-actions Bot added framework flutter/packages/flutter repository. See also f: labels. f: routes Navigator, Router, and related APIs. labels May 15, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a debug-time type check for Navigator results by adding the _debugCheckCanConsumeResult method to the Route class and integrating it into pop and maybePop. This change aims to provide clearer error messages when a route is popped with a mismatched result type. Feedback suggests using _lastRouteEntryWhereOrNull instead of lastWhere in the pop method to prevent a potential StateError if the navigation history is empty and to ensure consistency with the maybePop implementation.

Comment thread packages/flutter/lib/src/widgets/navigator.dart
justinmc
justinmc previously approved these changes May 15, 2026

@justinmc justinmc left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM minus a comment from Gemini. Thanks for making Flutter's error messages better!

Comment thread packages/flutter/lib/src/widgets/navigator.dart
Comment thread packages/flutter/lib/src/widgets/navigator.dart Outdated
@justinmc justinmc added the CICD Run CI/CD label May 15, 2026
Comment thread packages/flutter/lib/src/widgets/navigator.dart Outdated
Comment thread packages/flutter/lib/src/widgets/navigator.dart Outdated
Comment thread packages/flutter/test/widgets/navigator_test.dart Outdated
Comment thread packages/flutter/test/widgets/navigator_test.dart Outdated
Comment thread packages/flutter/test/widgets/navigator_test.dart Outdated
@puneetkukreja98

Copy link
Copy Markdown
Contributor Author

Addressed the review comments:

  • Removed Material dependencies from widget tests
  • Split pop and maybePop into separate tests
  • Updated type/null safety changes

@justinmc @navaronbracke could you please take another look?

justinmc
justinmc previously approved these changes May 18, 2026

@justinmc justinmc left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍 assuming the analyzer failure is fixed and tests pass.

navaronbracke
navaronbracke previously approved these changes May 18, 2026

@navaronbracke navaronbracke left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, modulo some typo fixes and something worth looking at in relation to the assert example

Comment thread packages/flutter/lib/src/widgets/navigator.dart Outdated
Comment thread packages/flutter/lib/src/widgets/navigator.dart Outdated
Comment thread packages/flutter/lib/src/widgets/navigator.dart
@navaronbracke

navaronbracke commented May 18, 2026

Copy link
Copy Markdown
Contributor

Linux analyze is failing on formatting

The Mac_x64 tool_tests_commands hit a crash that looks unrelated to your change, though (maybe rebase after fixing formatting?)

Windows framework_tests_misc seems to have been terminated on the bot?

victorsanni
victorsanni previously approved these changes May 18, 2026
@github-actions github-actions Bot removed the CICD Run CI/CD label May 19, 2026
navaronbracke
navaronbracke previously approved these changes May 19, 2026
@chunhtai chunhtai added the autosubmit Merge PR when tree becomes green via auto submit App label Jun 1, 2026
@auto-submit auto-submit Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Jun 1, 2026
@auto-submit

auto-submit Bot commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

autosubmit label was removed for flutter/flutter/186571, because - The status or check suite Google testing has failed. Please fix the issues identified (or deflake) before re-applying this label.

@github-actions github-actions Bot removed the CICD Run CI/CD label Jun 1, 2026
@chunhtai chunhtai added CICD Run CI/CD autosubmit Merge PR when tree becomes green via auto submit App labels Jun 1, 2026
@auto-submit auto-submit Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Jun 1, 2026
@auto-submit

auto-submit Bot commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

autosubmit label was removed for flutter/flutter/186571, because - The status or check suite Mac_x64 tool_tests_commands has failed. Please fix the issues identified (or deflake) before re-applying this label.

@chunhtai chunhtai added the autosubmit Merge PR when tree becomes green via auto submit App label Jun 1, 2026
@auto-submit auto-submit Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Jun 1, 2026
@auto-submit

auto-submit Bot commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

autosubmit label was removed for flutter/flutter/186571, because - The status or check suite Linux firebase_release_smoke_test has failed. Please fix the issues identified (or deflake) before re-applying this label.

@github-actions github-actions Bot removed the CICD Run CI/CD label Jun 2, 2026
@victorsanni victorsanni added CICD Run CI/CD autosubmit Merge PR when tree becomes green via auto submit App labels Jun 2, 2026
@auto-submit auto-submit Bot added this pull request to the merge queue Jun 2, 2026
Merged via the queue into flutter:master with commit 40135be Jun 2, 2026
94 checks passed
@flutter-dashboard flutter-dashboard Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Jun 2, 2026
auto-submit Bot pushed a commit to flutter/packages that referenced this pull request Jun 3, 2026
…11832)

Manual roll requested by tarrinneal@google.com

flutter/flutter@701665b...2ba5420

2026-06-03 engine-flutter-autoroll@skia.org Roll Packages from 818b310 to b11504f (8 revisions) (flutter/flutter#187511)
2026-06-03 mdebbar@google.com Add new file patterns for team-web labeler (flutter/flutter#187397)
2026-06-03 engine-flutter-autoroll@skia.org Roll Skia from 279b17fe9fc1 to d625048c853a (12 revisions) (flutter/flutter#187483)
2026-06-03 chris@bracken.jp [SwiftPM] Fix prefer_initializing_formals lint (flutter/flutter#187502)
2026-06-03 engine-flutter-autoroll@skia.org Roll Fuchsia Linux SDK from q27k7_um1GvVrySZS... to ap7MhLX4TdpWRrLS_... (flutter/flutter#187478)
2026-06-03 bkonyi@google.com [SwiftPM] Fix concurrent directory/file/symlink creation crashes (flutter/flutter#186953)
2026-06-03 flar@google.com [Impeller] Fix positioning of text shadow masks (flutter/flutter#187460)
2026-06-02 burak.karahan@mail.ru Remove Material imports from painting tests (flutter/flutter#186937)
2026-06-02 awolff@google.com Add android_hardware_smoke_test integration tests (flutter/flutter#187130)
2026-06-02 137456488+flutter-pub-roller-bot@users.noreply.github.com Roll pub packages (flutter/flutter#187471)
2026-06-02 pq@users.noreply.github.com [flutter tool] propagate analytics env to sub-tools (flutter/flutter#186780)
2026-06-02 30870216+gaaclarke@users.noreply.github.com Adds macro for fragment shaders to support flutter <= 3.44 (flutter/flutter#187316)
2026-06-02 116356835+AbdeMohlbi@users.noreply.github.com Small clean-up in different java files under `engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/` (flutter/flutter#186631)
2026-06-02 1961493+harryterkelsen@users.noreply.github.com refactor(web): Unify ui.Path code for CanvasKit and Skwasm (flutter/flutter#187331)
2026-06-02 engine-flutter-autoroll@skia.org Roll Packages from f5d50ca to 818b310 (2 revisions) (flutter/flutter#187441)
2026-06-02 faheemabbas766@gmail.com Allow selecting multi-digit device options (flutter/flutter#186184)
2026-06-02 puneetkukreja98@gmail.com Improve error message for type mismatch in Navigator.pop and maybePop. (flutter/flutter#186571)
2026-06-02 sanaullah.383@hotmail.com Remove semantics_tester import from material_button_test.dart (flutter/flutter#184807)
2026-06-02 bkonyi@google.com [flutter_tools] Refactor hostPlatform to use Abi.current() (flutter/flutter#185369)
2026-06-02 mvincentong@gmail.com Clean up avoid_type_to_string suppressions (flutter/flutter#186869)
2026-06-02 202459002+Lukes-Lair@users.noreply.github.com Update Flutter documentation links in flutter_console.bat (flutter/flutter#187354)
2026-06-02 154381524+flutteractionsbot@users.noreply.github.com Revert "[Impeller] Allow attaching specific texture mip levels and slices" (flutter/flutter#187445)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-packages
Please CC stuartmorgan@google.com,tarrinneal@google.com on the revert to ensure that a human
is aware of the problem.

To file a bug in Packages: https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
creatorpiyush pushed a commit to creatorpiyush/packages that referenced this pull request Jun 10, 2026
…lutter#11832)

Manual roll requested by tarrinneal@google.com

flutter/flutter@701665b...2ba5420

2026-06-03 engine-flutter-autoroll@skia.org Roll Packages from 818b310 to b11504f (8 revisions) (flutter/flutter#187511)
2026-06-03 mdebbar@google.com Add new file patterns for team-web labeler (flutter/flutter#187397)
2026-06-03 engine-flutter-autoroll@skia.org Roll Skia from 279b17fe9fc1 to d625048c853a (12 revisions) (flutter/flutter#187483)
2026-06-03 chris@bracken.jp [SwiftPM] Fix prefer_initializing_formals lint (flutter/flutter#187502)
2026-06-03 engine-flutter-autoroll@skia.org Roll Fuchsia Linux SDK from q27k7_um1GvVrySZS... to ap7MhLX4TdpWRrLS_... (flutter/flutter#187478)
2026-06-03 bkonyi@google.com [SwiftPM] Fix concurrent directory/file/symlink creation crashes (flutter/flutter#186953)
2026-06-03 flar@google.com [Impeller] Fix positioning of text shadow masks (flutter/flutter#187460)
2026-06-02 burak.karahan@mail.ru Remove Material imports from painting tests (flutter/flutter#186937)
2026-06-02 awolff@google.com Add android_hardware_smoke_test integration tests (flutter/flutter#187130)
2026-06-02 137456488+flutter-pub-roller-bot@users.noreply.github.com Roll pub packages (flutter/flutter#187471)
2026-06-02 pq@users.noreply.github.com [flutter tool] propagate analytics env to sub-tools (flutter/flutter#186780)
2026-06-02 30870216+gaaclarke@users.noreply.github.com Adds macro for fragment shaders to support flutter <= 3.44 (flutter/flutter#187316)
2026-06-02 116356835+AbdeMohlbi@users.noreply.github.com Small clean-up in different java files under `engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/` (flutter/flutter#186631)
2026-06-02 1961493+harryterkelsen@users.noreply.github.com refactor(web): Unify ui.Path code for CanvasKit and Skwasm (flutter/flutter#187331)
2026-06-02 engine-flutter-autoroll@skia.org Roll Packages from f5d50ca to 818b310 (2 revisions) (flutter/flutter#187441)
2026-06-02 faheemabbas766@gmail.com Allow selecting multi-digit device options (flutter/flutter#186184)
2026-06-02 puneetkukreja98@gmail.com Improve error message for type mismatch in Navigator.pop and maybePop. (flutter/flutter#186571)
2026-06-02 sanaullah.383@hotmail.com Remove semantics_tester import from material_button_test.dart (flutter/flutter#184807)
2026-06-02 bkonyi@google.com [flutter_tools] Refactor hostPlatform to use Abi.current() (flutter/flutter#185369)
2026-06-02 mvincentong@gmail.com Clean up avoid_type_to_string suppressions (flutter/flutter#186869)
2026-06-02 202459002+Lukes-Lair@users.noreply.github.com Update Flutter documentation links in flutter_console.bat (flutter/flutter#187354)
2026-06-02 154381524+flutteractionsbot@users.noreply.github.com Revert "[Impeller] Allow attaching specific texture mip levels and slices" (flutter/flutter#187445)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-packages
Please CC stuartmorgan@google.com,tarrinneal@google.com on the revert to ensure that a human
is aware of the problem.

To file a bug in Packages: https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
via-guy pushed a commit to via-guy/flutter that referenced this pull request Jun 26, 2026
flutter#186571)

<!--
Thanks for filing a pull request!
Reviewers are typically assigned within a week of filing a request.
To learn more about code review, see our documentation on Tree Hygiene:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
-->

**Context**
Currently, if a developer opens a strongly-typed route (such as
`showDialog<Null>`) but accidentally pops it with an incompatible value
(like `Navigator.pop(context, "CANCEL")`). Because the generic types do
not match, the Dart runtime instantly throws a cryptic native
`TypeError` (e.g., `type 'String' is not a subtype of type 'Null'`)
without any context about which route or widget caused it.

**Changes**
* Added a `_debugCheckCanConsumeResult` helper to `Route<T>` that
proactively validates `result is T?`.
* Intercepts `Navigator.pop`, `Navigator.maybePop`, before they modify
the stack or lock the Navigator.
* If a type mismatch occurs, it now throws a highly descriptive
`FlutterError` that pinpoints the exact expected type, the provided
type, and the specific `Route` object involved.
* Added comprehensive tests to `navigator_test.dart` to verify this
behavior for `pop` and `maybePop` using a real-world `showDialog`
scenario.

Fixes flutter#16987 



## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [AI contribution guidelines] and understand my
responsibilities, or I am not using AI tools.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [ ] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [x] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

If this change needs to override an active code freeze, provide a
comment explaining why. The code freeze workflow can be overridden by
code reviewers. See pinned issues for any active code freezes with
guidance.

**Note**: The Flutter team is currently trialing the use of [Gemini Code
Assist for
GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code).
Comments from the `gemini-code-assist` bot should not be taken as
authoritative feedback from the Flutter team. If you find its comments
useful you can update your code accordingly, but if you are unsure or
disagree with the feedback, please feel free to wait for a Flutter team
member's review for guidance on which automated comments should be
addressed.

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[AI contribution guidelines]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md

---------

Co-authored-by: chunhtai <47866232+chunhtai@users.noreply.github.com>
Co-authored-by: Victor Sanni <victorsanniay@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CICD Run CI/CD f: routes Navigator, Router, and related APIs. framework flutter/packages/flutter repository. See also f: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve error message for type error in maybePop()/pop()

6 participants