WillPopScope 在 Flutter 中已弃用

ahm*_*ary 16 mobile cross-platform deprecated dart flutter

“WillPopScope”已被弃用,不应使用。请改用 PopScope。此功能在 v3.12.0-1.0.pre 之后已弃用

WillPopScope(
  onWillPop: () async {
    // your logic        
    return false;
  },
)
Run Code Online (Sandbox Code Playgroud)

ahm*_*ary 24

解决了

将旧的小部件替换为WillPopScopePopScope的小部件检查下面的代码

/// NEW CODE
PopScope(
  canPop: true,
  onPopInvoked : (didPop){
   // logic
  },
)
Run Code Online (Sandbox Code Playgroud)


小智 18

你可以像这样使用它。

PopScope(
 canPop: false,
 onPopInvoked: (didPop) async {
  if (didPop) {
    return;
  }
  final navigator = Navigator.of(context);
  bool value = await someFunction();
  if (value) {
    navigator.pop();
  }
},
Run Code Online (Sandbox Code Playgroud)