1

I have a project that need to prevent user to refresh page with some conditions using flutter for web.

I already search and try the solution in this How to disable reloading or refresh button in browser in flutter web?

but i'm failed to cancel the refresh action.

here is my code to handling

html.window.onBeforeUnload.listen((event) async {
    if(true){
       event.preventDefault();
    }
});

Is there a way to cancel the refresh action?

Hansen
  • 650
  • 1
  • 11
  • 32

1 Answers1

0

You can use the universal_html library:

import 'package:universal_html/html.dart' as html;

...
bool condition = true;

@override
  void initState() {
    html.window.addEventListener('beforeunload', (event) {
      if (condition) {
        event.preventDefault();
        return 'Are you sure you want to leave?';
      }
      return null;
    });
    super.initState();
  }

Where condition is the condition for blocking the reload.

Leonardo Rignanese
  • 865
  • 11
  • 22