Current field guide · 2026
Leave a form deliberately
Protect unsaved work with a clear exit decision, and test every way a user can leave.
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.
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.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.
A dirty form can cancel go and pop
A focused field is not necessarily dirty. This complete local example compares the current title with the saved title, asks before leaving and preserves the edited text when the user chooses Keep editing. The title is synthetic and stays in memory; Save locally simulates a successful save, without a server or durable storage.
- Download the full project and retain its lockfile. It uses Flutter 3.47.8, Dart 3.13.7 and go_router 18.1.1. Start at
/edit. Changing “First draft” to a new title makes the form dirty; changing it back makes it clean. - Select Leave. The button calls
context.go('/done'), butGoRoute.onExitfirst awaits the decision. Keep editing returns false: the editor, typed text and URI remain. Discard returns true, resets the draft to its saved value and permits navigation. - Save locally copies the current title to the saved value. A later Leave needs no dialog. In a real application, update that saved value only after its save actually succeeds; the example does not implement a pending or failing network request.
- Start at
/, open the editor with push, edit and request pop. The same decision guards this exit. The shared pending Future prevents overlapping decisions. The dialog names the lost value, offers text buttons, and treats a null dismissal result as cancellation. - Run the six tests. They check clean go, cancel with text and URI preserved, discard with reset, local save, pushed-route pop cancellation then discard, and editing back to the saved value.
flutter pub get --offline
flutter analyze --no-pub
flutter test --no-pub
flutter build web --release --csp --no-web-resources-cdn --no-wasm-dry-run --no-pubChecked on 14 November 2026: analyze reported no issues, all six widget tests passed, and the release web build completed with the installed SDK and existing package cache. See the downloadable command/exit-code/log-hash record. These are Flutter checks, not a claim of a tested Android/iOS device or a browser-close interceptor.
For a browser session, also exercise Back after opening the editor, keyboard dialog choices, direct navigation and reload. A direct deep link starts a fresh app; a reload or closing the tab can discard this memory-only draft. Do not promise that onExit can intercept every browser or operating-system exit. Worksheet: saved value __; current value __; dirty __; requested destination __; decision __; resulting URI __.
Complete project, lockfile and six tests · ZIP · Commands and results · JSON