-
Notifications
You must be signed in to change notification settings - Fork 26.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[go_router] Fail to restore widget states after "restart and restore" when using go_router #117683
Comments
Thanks for the detailed report. Seeing the same behavior using the code sample provided. stable, master flutter doctor -v
|
Try this, it worked for me. Add pageBuilder instead of builder and set restorationId for MaterialPage: MaterialApp.router(
restorationScopeId: 'app',
routerConfig: GoRouter(
restorationScopeId: 'router',
routes: [
GoRoute(
path: '/',
pageBuilder: (context, state) => const MaterialPage(
restorationId: 'router.scroll',
child: ScrollScreen()
)
),
],
),
); |
Thanks @Ivan-Lytvynenko your example works for me as well. I think we should update the sample in https://github.com/flutter/packages/blob/main/packages/go_router/example/lib/others/state_restoration.dart to use pageBuilder instead. |
I've found a neat workaround for missing implementation of state restoration on web platform, see #65777 (comment). |
I've found another workaround. Use subtype of class _RestorableGoRoute extends GoRoute {
_RestorableGoRoute ({
required super.path,
super.builder,
super.name,
super.pageBuilder,
super.parentNavigatorKey,
super.redirect,
super.routes,
});
@override
int get hashCode => path.hashCode;
} Result of studyThe direct cause is
|
Steps to Reproduce
Given two similar
MaterialApp
, one usinggo_router
and one who doesn't, the one usinggo_router
is unable to restore the screen state when the app is restarted.The best way to see this problem is by running the included code sample.
Their implementation is very similar:
and:
The one using
GoRouter
is unable to restore the state of theScrollScreen
when the app is restarted.You can see this behavior with the attached widget tests in the "code" section.
I've looked for similar reported issues, but I couldn't find one that explains this exact problem:
GoRouter
, which does not restore the state of the Pages, only the state of theGoRouter
(e.g. the location)go_router
, at first I have thought this was a problem withShellRoute
but I have reduced the problem to a minimal example that doesn't useShellRoute
. Link to the WIP PR: Migrateveggieseasons
togo_router
samples#1544Expected results:
Both cases should work the same way. The screen state should be restored after restart just like when using a
MaterialApp
withoutGoRouter
Actual results:
In this case, the state of the screen when using
GoRouter
is not restored, and this can be seen by using the included widget tests.Code sample
Add
go_router
to your project, then paste the following three classes into your main.dart.Also, copy the two widget tests into your widget_test.dart to run them.
First class is the
AppGoRoute
, which is a simpleMaterialApp
that usesGoRouter
.The second class is
AppSimple
, which is a simpleMaterialApp
that doesn't useGoRouter
.ScrollScreen
is a simpleScaffold
with aListView
of a 1000 items.Test 1: Fails: This test fails, as when the screen state is restored, the scroll position is lost and is back at the item 0.
This test uses the
AppGoRoute
, which usesGoRouter
internally to handle navigation.Test 2: Passes.
This test uses
AppSimple
, same test content, and the test passes.Logs
These are the test results:
The text was updated successfully, but these errors were encountered: