怎么从WillPopScope迁移到PopScope

文章讲述了在将Flutter升级到3.22.0-0.1.pre版本时,开发者遇到WillPopScope被弃用的问题,转向使用PopScope带来的变化,以及如何在新的PopScope中实现异步判断是否pop的困扰与解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近把flutter升级到了3.22.0-0.1.pre,3.19的正式版在我的项目上有些问题,先到预览版过渡一下.

发现WillPopScope被弃用了:

(
  'Use PopScope instead. '
  'This feature was deprecated after v3.12.0-1.0.pre.',
)
class WillPopScope extends StatefulWidget {

迁移到了PopScope,但是PopScope接受的是否pop的参数和WillPopScope相比变化较大,

WillPopScope:

  /// Called to veto attempts by the user to dismiss the enclosing [ModalRoute].
  ///
  /// If the callback returns a Future that resolves to false, the enclosing
  /// route will not be popped.
  final WillPopCallback? onWillPop;
  
  typedef WillPopCallback = Future<bool> Function();
  

本来是接收一个返回Future 的callback的,方便我们在里面进行一些异步的操作判断当前是否需要pop,用起来是非常方便的.但是PopScope的改成了:

  /// {@template flutter.widgets.PopScope.canPop}
  /// When false, blocks the current route from being popped.
  ///
  /// This includes the root route, where upon popping, the Flutter app would
  /// exit.
  ///
  /// If multiple [PopScope] widgets appear in a route's widget subtree, then
  /// each and every `canPop` must be `true` in order for the route to be
  /// able to pop.
  ///
  /// [Android's predictive back](https://developer.android.com/guide/navigation/predictive-back-gesture)
  /// feature will not animate when this boolean is false.
  /// {@endtemplate}
  final bool canPop;
  
  
    /// {@template flutter.widgets.PopScope.onPopInvoked}
  /// Called after a route pop was handled.
  /// {@endtemplate}
  ///
  /// It's not possible to prevent the pop from happening at the time that this
  /// method is called; the pop has already happened. Use [canPop] to
  /// disable pops in advance.
  ///
  /// This will still be called even when the pop is canceled. A pop is canceled
  /// when the relevant [Route.popDisposition] returns false, such as when
  /// [canPop] is set to false on a [PopScope]. The `didPop` parameter
  /// indicates whether or not the back navigation actually happened
  /// successfully.
  ///
  /// See also:
  ///
  ///  * [Route.onPopInvoked], which is similar.
  final PopInvokedCallback? onPopInvoked;


typedef PopInvokedCallback = void Function(bool didPop);

直接接收一个bool,和一个不好用的回调,不能再愉快的进行异步调用判断是否要pop了,个人觉得是反向升级.那么在PopScope下怎么进行异步判断呢?

    return WillPopScope(
      onWillPop: () async {
        return await checkHasNoUpload();
 	},
 	child:child
 	};
 	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 	 return PopScope(
      canPop: false,
      onPopInvoked: (didPop) async {
        if(didPop){
          return;
        }
        if (await checkHasNoUpload()) {
          Get.back();
        }
      },
      child:child
 	};
 	

麻烦了不少.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值