Quantcast
Viewing all articles
Browse latest Browse all 103

Answer by mikyll98 for catch Android back button event on Flutter

WillPopScope is now deprecated, and shouldn't be used anymore. Reference: Android Predictive Back

To support Android 14’s Predictive Back feature, a set of ahead-of-time APIs have replaced just-in-time navigation APIs, like WillPopScope and Navigator.willPop.

If you try using it, you'll get the following warning:

'WillPopScope.new' is deprecated and shouldn't be used.Use PopScope instead. This feature was deprecated after v3.12.0-1.0.pre.Try replacing the use of the deprecated member with the replacement.

Replacement

If you're using version 3.14.0-7.0.pre of Flutter or greater, you should replace WillPopScope widgets with PopScope. It has 2 fields:

  • bool canPop, enables/disables system back gestures (pop);
  • void Function(bool didPop)? onPopInvoked, is a callback that's always invoked on system back gestures, even if canPop is set to false, but in this case the route will not be popped off, and didPop will be set to false (so you can check its value to discriminate the logic).

Migration Example

With WillPopScope:

WillPopScope(  onWillPop: () async {    final bool? shouldPop = await _showBackDialog();    return shouldPop ?? false;  },  child: child,)

With PopScope:

PopScope(  canPop: false,  onPopInvoked: (bool didPop) async {    if (didPop) {      return;    }    final NavigatorState navigator = Navigator.of(context);    final bool? shouldPop = await _showBackDialog();    if (shouldPop ?? false) {      navigator.pop();    }  },  child: child,)

Viewing all articles
Browse latest Browse all 103

Trending Articles