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添加对应的参数:
之后在onTap事件中传入该参数: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: () { GoRouter.of(context) .go('/detailed_post/${widget.title}/${widget.description}'); },
内容的提问来源于stack exchange,提问作者ngqh
相关产品推荐
相关产品推荐

