美文网首页跨端开发录
【Flutter】GoRouter使用问题集绵

【Flutter】GoRouter使用问题集绵

作者: 热爱技术的小卢同学 | 来源:发表于2025-01-02 10:04 被阅读0次
  • WillPopScope失效:

在项目使用go_router插件后,WillPopScope这个组件被自动屏蔽掉了无法正常使用,官方给出的说明是这样的

GoRouter and other Router-based APIs are not compatible with the WillPopScope widget.

//GoRouter以及相关的其他基础api均不兼容WillPopScope组件

See issue #102408 for details on what such an API might look like in go_router.

  • 解决方案:

在gorouter配置类中使用onExit回调方法代替

GoRouter router = GoRouter(
  routes: [
    // 首页
    GoRoute(
        path: '/home',
        name: '/home',
        pageBuilder: (context, state) {
          return 你的页面组件;
        },
        onExit: (context, state) async {
          if(满足需要pop的条件){
              //通过 return true来允许pop
              return true;
          }
          ....//其他操作
          //不进行pop
          return false;
        }
    )
  ]
)

相关文章

网友评论

    本文标题:【Flutter】GoRouter使用问题集绵

    本文链接:https://www.haomeiwen.com/subject/rngfyjtx.html