You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter Web调用Navigator.pop返回页面时出现PersistedOffset异常求助

问题描述

点击arrow_back图标调用Navigator.pop(context)返回上一页时,出现错误提示:PersistedOffset: is in an unexpected state.,当前使用Flutter Dev通道,运行设备为Web Chrome。

相关代码如下:

跳转至IndividualDetails页面的CustomCard组件代码

class CustomCard extends StatelessWidget {
  const CustomCard({super.key, required this.chatModel});
  final ChatModel chatModel;

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () => Navigator.of(context).push(MaterialPageRoute(
          builder: (context) => IndividualDetails(
                chatModel: chatModel,
              ))),
      child: Column(
        children: [
          ListTile(
            leading: CircleAvatar(
              radius: 30,
              backgroundColor: Colors.blueGrey,
              child: SvgPicture.asset(
                chatModel.isGroup ? 'assets/groups.svg' : 'assets/person.svg',
                color: Colors.white,
                height: 32,
                width: 32,
              ),
            ),
            trailing: Text(chatModel.time),
            title: Text(
              chatModel.name,
              style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
            ),
            subtitle: Row(
              children: [
                const Icon(
                  Icons.done_all,
                  color: Colors.blue,
                  size: 20,
                ),
                const SizedBox(
                  width: 2.0,
                ),
                Text(chatModel.currentMessage),
              ],
            ),
          ),
          const Padding(
            padding: EdgeInsets.only(left: 80.0, right: 20.0),
            child: Divider(
              thickness: 1.5,
            ),
          ),
        ],
      ),
    );
  }
}

IndividualDetails页面返回按钮代码

class IndividualDetails extends StatefulWidget {
  const IndividualDetails({super.key, required this.chatModel});
  final ChatModel chatModel;
  @override
  State<IndividualDetails> createState() => _IndividualDetailsState();
}
class _IndividualDetailsState extends State<IndividualDetails> {
  @override
Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leadingWidth: 70,
        leading: InkWell(
          onTap: () {
             Navigator.of(context).pop();
          },
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: const [
              Icon(
                Icons.arrow_back,
                size: 24,
              ),
              CircleAvatar(
                radius: 20,
                backgroundColor: Colors.blueGrey,
              child: SvgPicture.asset(
                widget.chatModel.isGroup ? 'assets/groups.svg' : 'assets/person.svg',
                color: Colors.white,
                height: 32,
                width: 32,
              ),
              ),
            ],
          ),
        ),
      ),
    );
  }
 }

需求:点击返回按钮能正常回到CustomCard所在页面,解决该异常。


解决方案

这个问题是Flutter Dev通道在Web端的已知兼容性问题,和滚动视图的偏移持久化逻辑冲突有关,可尝试以下几种解决方式:

  • 切换至稳定版Flutter通道:
    执行命令切换到稳定通道,避开Dev通道的未修复bug:

    flutter channel stable
    flutter upgrade
    
  • 延迟执行返回逻辑:
    在返回时添加短暂延迟,让页面状态稳定后再执行pop操作:

    onTap: () {
      Future.delayed(const Duration(milliseconds: 100), () {
        Navigator.of(context).pop();
      });
    },
    
  • 使用AppBar默认返回按钮:
    暂时移除自定义leading,改用AppBar默认的返回按钮,避免自定义布局触发的状态问题:

    appBar: AppBar(
      leadingWidth: 70,
      title: Row(
        children: [
          CircleAvatar(
            radius: 20,
            backgroundColor: Colors.blueGrey,
            child: SvgPicture.asset(
              widget.chatModel.isGroup ? 'assets/groups.svg' : 'assets/person.svg',
              color: Colors.white,
              height: 32,
              width: 32,
            ),
          ),
          // 可添加标题等其他内容
        ],
      ),
    ),
    
  • 禁用滚动偏移持久化:
    如果CustomCard所在页面包含ListView等滚动组件,在跳转路由时添加restoreScrollPosition: false参数:

    Navigator.of(context).push(MaterialPageRoute(
      builder: (context) => IndividualDetails(chatModel: chatModel),
      restoreScrollPosition: false,
    ));
    

内容的提问来源于stack exchange,提问作者jack

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 16:39:45