如何解决在onPressed操作期间未在Flutter登录表单中找到路由
我使用名为ChatPage.id的Routes创建了一个注册表单和登录表单,并使用pushNamed函数更改了屏幕,但是在按操作时,用户是在Firebase上创建的,但是我没有进行屏幕更改,并且生成的错误是:>
I/flutter (14603): Could not find a generator for route RouteSettings("/chatpage",null) in the _WidgetsAppState.
I/flutter (14603): Generators for routes are searched for in the following order:
I/flutter (14603): 1. For the "/" route,the "home" property,if non-null,is used.
I/flutter (14603): 2. Otherwise,the "routes" table is used,if it has an entry for the route.
I/flutter (14603): 3. Otherwise,onGenerateRoute is called. It should return a non-null value for any valid route not handled by "home" and "routes".
I/flutter (14603): 4. Finally if all else fails onUnknownRoute is called.
I/flutter (14603): Unfortunately,onUnknownRoute was not set.
这是我的代码:
class RegistrationScreen extends StatefulWidget {
static const String id = 'registration_screen';
@override
_RegistrationScreenState createState() => _RegistrationScreenState();
}
class _RegistrationScreenState extends State<RegistrationScreen> {
final _formKey = GlobalKey<FormState>();
final _auth = FirebaseAuth.instance;
bool showSpinner = false;
String
name,lastName,email,password;
final databaseReference = Firestore.instance;
bool rememberMe = false;
bool showErrorMessage = false;
void createRecord() async {
await databaseReference.collection("user")
.add({
'name': '$name','lastName': '$lastName','email': '$email','password': 'password',}).then((value) {
print(value.documentID);
});
}
and the action:
onPressed: rememberMe ? () async {
try {
final newUser =
await _auth.createUserWithEmailAndPassword(
email: email,password: password);
if (newUser != null) {
Navigator.pushNamed(context,ChatPage.id);
}
如果我使用pushReplacement可以工作,但是我可以将ID添加到ChatPage
解决方法
该错误表示未找到route
,您需要先定义路线示例:
initialRoute: '/',routes: {
// When navigating to the "/" route,build the FirstScreen widget.
'/': (context) => FirstScreen(),// When navigating to the "/second" route,build the SecondScreen widget.
'/second': (context) => SecondScreen(),},
然后,您需要在route
中使用pushNamed
的名称,如果还要传递参数,则可以使用属性argument
将参数发送到第二个屏幕:
onPressed: () {
// Navigate to the second screen using a named route.
Navigator.pushNamed(context,'/second',arguments: "test");
},
在SecondScreen()
中,您可以收到参数:
class SecondScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
var arguments = ModalRoute.of(context).settings.arguments;
print(arguments);
return Scaffold(
//...
https://flutter.dev/docs/cookbook/navigation/named-routes
https://flutter.dev/docs/cookbook/navigation/navigate-with-arguments
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。