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.