GoRouter.dev

Current field guide · 2026

Leave a form deliberately

Protect unsaved work with a clear exit decision, and test every way a user can leave.

Edition published 21 Oct 2026go_router 18.1.1 reference
On this page

Model dirty state explicitly

Keep track of whether edits differ from the saved value. A screen with unsaved input should not be treated the same as a screen that has merely been focused. Reset the flag only after a save succeeds or the user explicitly discards changes.

Make the confirmation specific

Tell the user what will be lost. Offer a safe action that preserves editing, and avoid stacking several confirmation dialogs during one navigation attempt. If saving takes time, make the pending state visible.

Use the supported exit mechanism

Current GoRoute exposes onExit for a route-level exit decision, including an asynchronous decision. Flutter also provides PopScope for pop handling. Consult both APIs for your Flutter version; the old WillPopScope examples in the historical guide are not a universal modern solution.

Test beyond the visible Back button

Exercise programmatic navigation, a tab switch, Android Back, browser history, and a reload as appropriate. A route-exit callback is not a promise that every browser or operating-system close event can be intercepted.

dart / example
GoRoute(
  path: '/edit',
  builder: (_, __) => const EditorScreen(),
  onExit: (context, state) async {
    if (!editor.hasUnsavedChanges) return true;
    return await confirmDiscard(context);
  },
);
// editor and confirmDiscard are application-owned.
Earlier edition: read the complete original guide. Its older APIs are kept in historical context.

Sources & version context

GoRouter.dev documentation edition. Examples illustrate the APIs described in the linked primary documentation. Adapt application-owned widgets and services to your project. They are not an executed Flutter session in this browser.

Find your next route