|
| 1 | +## What is state restoration? |
| 2 | + |
| 3 | +State restoration refers to the process of persisting and restoring serialized state |
| 4 | +after the app has been killed by the operating system in the background. |
| 5 | + |
| 6 | +For more information, see [Restore state on Android](https://docs.flutter.dev/platform-integration/android/restore-state-android) |
| 7 | +and [Restore state on iOS](https://docs.flutter.dev/platform-integration/ios/restore-state-ios). |
| 8 | + |
| 9 | +> [!NOTE] |
| 10 | +> State restoration does not refer to general purpose state persistence. |
| 11 | +> For keeping multiple navigation branches in memory at the same time, |
| 12 | +> see [StatefulShellRoute](https://pub.dev/documentation/go_router/latest/go_router/StatefulShellRoute-class.html). |
| 13 | +
|
| 14 | +## Support |
| 15 | + |
| 16 | +GoRouter fully supports state restoration. |
| 17 | + |
| 18 | +To enable state restoration, a top-level configuration is needed |
| 19 | +as well as additional configuration depending on the types of routes used. |
| 20 | + |
| 21 | +## Top-level configuration |
| 22 | + |
| 23 | +Add `restorationScopeId`s to `GoRouter` and `MaterialApp.router`: |
| 24 | + |
| 25 | +```dart |
| 26 | +final _router = GoRouter( |
| 27 | + restorationScopeId: 'router', |
| 28 | + routes: [ |
| 29 | + ... |
| 30 | + ], |
| 31 | +); |
| 32 | +``` |
| 33 | + |
| 34 | +```dart |
| 35 | +class MyApp extends StatelessWidget { |
| 36 | + @override |
| 37 | + Widget build(BuildContext context) { |
| 38 | + return MaterialApp.router( |
| 39 | + restorationScopeId: 'app', |
| 40 | + routerConfig: _router, |
| 41 | + ); |
| 42 | + } |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +## Route-specific configuration |
| 47 | + |
| 48 | +### GoRoute |
| 49 | + |
| 50 | +For a `GoRoute` that uses `pageBuilder`, supply a `restorationId` to the page: |
| 51 | + |
| 52 | +```dart |
| 53 | +GoRoute( |
| 54 | + pageBuilder: (context, state) { |
| 55 | + return MaterialPage( |
| 56 | + restorationId: 'detailsPage', |
| 57 | + path: '/details', |
| 58 | + child: DetailsPage(), |
| 59 | + ); |
| 60 | + }, |
| 61 | +) |
| 62 | +``` |
| 63 | + |
| 64 | +For a `GoRoute` that does not use `pageBuilder`, no additional configuration |
| 65 | +is needed. |
| 66 | + |
| 67 | +For a runnable example with tests, see the [GoRoute state restoration example](https://github.com/flutter/packages/tree/main/packages/go_router/example/lib/state_restoration/go_route_state_restoration.dart). |
| 68 | + |
| 69 | +### ShellRoute |
| 70 | + |
| 71 | +Add a unique `restorationScopeId` to the `ShellRoute`. |
| 72 | +Additionally, add a `pageBuilder` and supply a `restorationId` to the page. |
| 73 | + |
| 74 | +> [!IMPORTANT] |
| 75 | +> A `pageBuilder` which returns a page with `restorationId` must be supplied for `ShellRoute` state restoration to work. |
| 76 | +
|
| 77 | +For a runnable example with tests, see the [ShellRoute state restoration example](https://github.com/flutter/packages/tree/main/packages/go_router/example/lib/state_restoration/shell_route_state_restoration.dart). |
| 78 | + |
| 79 | +```dart |
| 80 | +ShellRoute( |
| 81 | + restorationScopeId: 'onboardingShell', |
| 82 | + pageBuilder: (context, state, child) { |
| 83 | + return MaterialPage( |
| 84 | + restorationId: 'onboardingPage', |
| 85 | + child: OnboardingScaffold(child: child), |
| 86 | + ); |
| 87 | + }, |
| 88 | + routes: [ |
| 89 | + ... |
| 90 | + ], |
| 91 | +) |
| 92 | +``` |
| 93 | + |
| 94 | +### StatefulShellRoute |
| 95 | + |
| 96 | +Add a `restorationScopeId` to the `StatefulShellRoute` and a |
| 97 | +`pageBuilder` which returns a page with a `restorationId`. |
| 98 | + |
| 99 | +Additionally, add a `restorationScopeId` to each `StatefulShellBranch`. |
| 100 | + |
| 101 | +> [!IMPORTANT] |
| 102 | +> A `pageBuilder` which returns a page with `restorationId` must be supplied for `StatefulShellRoute` state restoration to work. |
| 103 | +
|
| 104 | +For a runnable example with tests, see the [StatefulShellRoute state restoration example](https://github.com/flutter/packages/tree/main/packages/go_router/example/lib/state_restoration/stateful_shell_route_state_restoration.dart). |
| 105 | + |
| 106 | +```dart |
| 107 | +StatefulShellRoute.indexedStack( |
| 108 | + restorationScopeId: 'appShell', |
| 109 | + pageBuilder: (context, state, navigationShell) { |
| 110 | + return MaterialPage( |
| 111 | + restorationId: 'appShellPage', |
| 112 | + child: AppShell(navigationShell: navigationShell), |
| 113 | + ); |
| 114 | + }, |
| 115 | + branches: [ |
| 116 | + StatefulShellBranch( |
| 117 | + restorationScopeId: 'homeBranch', |
| 118 | + routes: [ |
| 119 | + ... |
| 120 | + ], |
| 121 | + ), |
| 122 | + StatefulShellBranch( |
| 123 | + restorationScopeId: 'profileBranch', |
| 124 | + routes: [ |
| 125 | + ... |
| 126 | + ], |
| 127 | + ), |
| 128 | + ], |
| 129 | +) |
| 130 | +``` |
0 commit comments