-
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
Flutter Web: Refreshing browser resets navigation stack and local variables #59277
Comments
Code Sample
flutter doctor -v
flutter doctor -v
|
When you refresh the page, the entire app is destroyed and launched again. This is how the browser works, nothing specific to Flutter here. So the values of local variables are expected to disappear. The way navigation works today is by keeping the navigation stack in memory. This means the stack is destroyed on page refresh, and the history is lost. There are a few projects coming along that will help with these issues:
|
Any updates on this? There seems to be, at least a partial implementation for the Navigator 2.0 in the latest release but I'm not sure if everything needed to reset the navigation stack is there. |
Any update on this? I do believe it is quite strange that there still is no solution to this obvious problem |
Do you still see the issue if you use Router? |
It's unfortunately refreshing the entire app, not only navigation stack |
I think this problem by this issue https://bugs.chromium.org/p/chromium/issues/detail?id=1098388. |
Might not be entirely useful, but here's a stackoverflow suggestion from the dart:html package that detects when the page is being refreshed or closed (tested on my end and it works). My current working theory is that if I can detect when the page is being refreshed, I can set the local properties to previous values. Haven't completely figured it out yet, just thought I'd drop this if it's useful for anyone else. https://stackoverflow.com/questions/62237262/flutter-web-detect-browser-tab-close-or-refresh |
Followup, I think I've found a semi-promising but incomplete solution to the browser back arrow creating weird results issue after refreshing leads to the Navigator stack getting cleared. Essentially I set up the
(The existing page that's being refreshed has a route name of '/charities'). What this does is it stacks a new navigation stack and disables the back button from being pressed so the funky browser back arrow action can't happen. I know it's not perfect but I think it's somewhat of a start. Please do lmk if anyone comes up with a better solution. |
Please excuse this comment from a Flutter novice. I'll delete it if folks believe it is just noise but I'm writing it because I think it may point out an aspect of this issue that doesn't appear to be mentioned in previous comments. I just encountered this and it appears that my problem is the Provider.of call in the route that is on top of the route stack at the time the user clicks reload no longer returns the proper data. After the restart caused by the reload, Luckily--and somewhat to my surprise--the restart does not log the user out of FirebaseAuth so I should be able to update the shared object after the restart. I'm going to first try doing this in the default constructor used by * I didn't do it this way initially because "of course" there is no active user when the app starts up :-). UpdateIssue 3356 -- [firebase_auth] on web authStateChanges sink null value then correct authState on page reload may be related to this one. |
Any update? For my app, when browser's refresh button clicked, it should stay at same page, but when close button clicked, next time when reopen it, it should go to sign in page. html.window.onBeforeUnload.listen can not work on this case. |
@LiangHuangBC could you share your code ? |
Flutter treats page refresh on web as an app restart. What I did as a workaround is I keep track of my current page in SharedPreferences. On page refresh, app checks if there's a page stored in SharedPreferences. If yes, it then the stored page was pushed. Otherwise, the default page will be displayed. Keep tabs on the current page by calling @override
void initState(){
super.initState();
...
getCurrentPage().then((currentPageState) {
_navigatorKey.currentState!.pushReplacementNamed(currentPageState);
}
}
/// Fetch stored page. Display default page if null.
Future<String> getCurrentPage() async {
var pref = await SharedPreferences.getInstance();
return pref.getString(SharedPrefKey.currentPage) ?? NavigatorHelper.home;
}
/// Store current [page] to restore into during page refresh on web
Future<void> setCurrentPage(String page) async {
var pref = await SharedPreferences.getInstance();
pref.setString(SharedPrefKey.currentPage, page);
} If named routes was used, the Navigation Stack for the previous named routes should persist on page refresh.
|
Any updates? As far as I am aware (please correct me if I'm wrong), there is nothing in the Navigator 2.0 API which allows developers to solve this problem, which seems quite a major one. |
Waiting for Navigator 3.0 to solve this 🐞. |
Was anyone able to solve this? |
same issue + 1 |
Any updates? |
Same issue. I think this should be the top issue for flutter web cause it's really a bad experience for h5 users. |
biggest issue |
Same Issue + 1 |
Same issue :( |
Same issue here guys. |
Same Issue + 1 |
Can be solved by #65777 (comment) |
Same Issue + 1 |
same issue, any update? |
Same Issue + 1 |
any update ? |
any update? |
Facing same issue, any update? |
any update ? |
any update? |
1 similar comment
any update? |
any update? +1 |
Folks, this is expected behavior on the web. When you refresh the page, the app starts from scratch with all state cleared. Flutter has the concept of state restoration which can help here, but it's not implemented for the web (yet). Please follow along in the corresponding issue (and add your thumbs ups there). State restoration for web: #65777 I'm closing this issue because there's no extra value to be provided here. |
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of |
Steps to Reproduce
git clone https://github.com/huffSamuel/flutter_browser_navigation.git
Expected results:
Refreshing the browser refreshes the current page. Navigator stack and local variables are not cleared.
Actual results:
Current page refreshes but all local properties are null. Navigator stack is cleared so pressing the back arrow results in odd behavior.
Logs
The text was updated successfully, but these errors were encountered: