Flutter页面导航报错:必填参数documentSnapshot未传递
问题:跳转Firebase集成页面时缺少必填参数报错
报错提示:The named parameter 'documentSnapshot' is required, but there's no corresponding argument
场景:从未集成Firebase的首页点击导航项,跳转至集成Firebase的tourist页面时触发该错误。首页跳转代码未传递参数,而tourist页面的StatefulWidget声明了必填的documentSnapshot参数,但该参数在tourist页面内部从未被使用。
解决方法
1. 清理tourist页面的无用参数
tourist页面的documentSnapshot参数完全未参与业务逻辑,直接删除相关代码即可:
class Tourist extends StatefulWidget { // 移除原有的documentSnapshot参数及required修饰符 const Tourist({super.key}); @override State<Tourist> createState() => _TouristState(); }
注:Dart规范要求类名首字母大写,建议将
tourist改为Tourist。
2. 修改首页的跳转代码
现在tourist页面无需参数,直接正常跳转:
onTap: (){ Navigator.push(context, MaterialPageRoute(builder: (context) => const Tourist())); },
补充说明
若之前仅移除required修饰符仍报错,是因为只取消了必填限制,但未删除未初始化的参数定义。正确操作是直接删除整个documentSnapshot参数相关代码,因为该参数在页面逻辑中没有实际作用。
内容的提问来源于stack exchange,提问作者Ohood Al-Ismaili
相关产品推荐
相关产品推荐

