怎么从WillPopScope迁移到PopScope

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

最近把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
 	};
 	

麻烦了不少.

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将C++代码从Visual Studio迁移到VSCode,您可以按照以下步骤操作: 1. 安装VSCode:首先,确保您已在计算机上安装了VSCode编辑器。您可以从VSCode官方网站(https://code.visualstudio.com/)下载并安装适用于您操作系统的版本。 2. 安装C++扩展:在VSCode中,点击左侧的扩展图标 (四个方块)。在搜索栏中输入"C++",然后选择由Microsoft提供的"C++"扩展进行安装。 3. 打开代码文件夹:在VSCode中,选择"文件" -> "打开文件夹",然后导航到您的C++代码所在的文件夹并选择打开。 4. 创建或编辑调试配置文件:在VSCode中,点击左侧的调试图标 (虫子)。然后点击"创建一个 launch.json 文件"链接以创建一个新的调试配置文件,或者编辑现有的配置文件。 5. 配置调试器:根据您的项目类型和需求,选择适当的调试配置。例如,如果您使用的是GCC编译器,您可以选择"cppdbg: Launch"配置。根据您的项目路径和编译器设置,对调试配置进行相应的修改。 6. 构建和调试:使用VSCode的终端或集成终端,在代码目录中运行适当的构建命令来编译您的C++代码。然后,您可以在VSCode中点击调试图标旁边的"启动调试"按钮来开始调试您的代码。 请注意,迁移过程可能涉及到其他配置和调整,具体取决于您的项目和开发环境。此外,如果您的项目依赖于其他库或框架,您还需要在VSCode中设置相应的构建和链接选项。 希望这些步骤可以帮助您成功地将C++代码从Visual Studio迁移到VSCode。如有任何问题,请随时向我提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值