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

Flutter使用go_router从列表页向详情页传递标题数据

解决方案

你的路由路径/detailed_post/:title/:description定义了两个必填参数:title和:description,调用go方法时需要按顺序传入这两个参数的实际值。

直接修改后的onTap代码

onTap: () {    
  GoRouter.of(context)
          .go('/detailed_post/${widget.title}/替换为实际的描述内容');
},

补充说明

  • 在PostCard的State类中,需要通过widget.title来访问父组件传入的title参数
  • 如果需要传递真实的描述信息,首先要给PostCard添加对应的参数:
    class PostCard extends StatefulWidget {
      const PostCard({super.key, required this.title, required this.description});
      final String title;
      final String description; // 新增描述参数
    
      @override
      State<PostCard> createState() => _PostCardState();
    }
    
    之后在onTap事件中传入该参数:
    onTap: () {    
      GoRouter.of(context)
              .go('/detailed_post/${widget.title}/${widget.description}');
    },
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 19:34:55