firebase - Flutter Firebase - 注册后无法导航到登录屏幕
问题描述
我希望用户在注册后返回登录页面。
我的代码有效,用户注册,但应用程序没有导航到登录页面。
注册页面代码:
import 'package:flutter/material.dart';
import 'package:iddmib_rev/screens/haber_screen.dart';
import 'package:iddmib_rev/screens/login_screen.dart';
import 'package:iddmib_rev/services/auth.dart';
class RegisterScreen extends StatefulWidget {
@override
_RegisterScreenState createState() => _RegisterScreenState();
}
class _RegisterScreenState extends State<RegisterScreen> {
final TextEditingController _nameController = TextEditingController();
final TextEditingController _emailController = TextEditingController();
final TextEditingController _passwordController = TextEditingController();
final TextEditingController _passwordAgainController =
TextEditingController();
AuthService _authService = AuthService();
@override
Widget build(BuildContext context) {
return Scaffold(
body: SingleChildScrollView(
child: Container(
decoration: BoxDecoration(
color: Colors.grey[100],
),
height: MediaQuery.of(context).size.height,
child: Column(
children: <Widget>[
Image(
height: MediaQuery.of(context).size.height / 3.5,
image: AssetImage('assets/images/logo.png'),
),
Padding(
padding: EdgeInsets.symmetric(horizontal: 60.0, vertical: 10.0),
child: TextField(
controller: _nameController,
decoration: InputDecoration(
contentPadding: EdgeInsets.symmetric(vertical: 25.0),
fillColor: Colors.white,
filled: true,
hintText: 'İsim',
prefixIcon: Icon(
Icons.person_outlined,
size: 30.0,
),
),
),
),
Padding(
padding: EdgeInsets.symmetric(horizontal: 60.0, vertical: 10.0),
child: TextField(
controller: _emailController,
decoration: InputDecoration(
contentPadding: EdgeInsets.symmetric(vertical: 25.0),
fillColor: Colors.white,
filled: true,
hintText: 'E-posta',
prefixIcon: Icon(
Icons.email_outlined,
size: 30.0,
),
),
),
),
Padding(
padding: EdgeInsets.symmetric(horizontal: 60.0, vertical: 10.0),
child: TextField(
controller: _passwordController,
obscureText: true,
decoration: InputDecoration(
contentPadding: EdgeInsets.symmetric(vertical: 25.0),
fillColor: Colors.white,
filled: true,
hintText: 'Şifre',
prefixIcon: Icon(
Icons.password_outlined,
size: 30.0,
),
),
),
),
Padding(
padding: EdgeInsets.symmetric(horizontal: 60.0, vertical: 10.0),
child: TextField(
controller: _passwordAgainController,
obscureText: true,
decoration: InputDecoration(
contentPadding: EdgeInsets.symmetric(vertical: 25.0),
fillColor: Colors.white,
filled: true,
hintText: 'Şifre Tekrar',
prefixIcon: Icon(
Icons.password_outlined,
size: 30.0,
),
),
),
),
SizedBox(height: 10.0),
InkWell(
onTap: () {
_authService
.createPerson(_nameController.text, _emailController.text,
_passwordController.text)
.then((value) {
return Navigator.push(context,
MaterialPageRoute(builder: (context) => LoginScreen()));
});
},
child: Container(
margin: EdgeInsets.symmetric(horizontal: 60.0),
alignment: Alignment.center,
height: 60.0,
decoration: BoxDecoration(
color: Theme.of(context).primaryColor,
borderRadius: BorderRadius.circular(5.0),
),
child: Text(
'Kayıt Ol',
style: TextStyle(
color: Colors.white,
fontSize: 25.0,
),
),
),
),
SizedBox(height: 10.0),
],
),
)),
);
}
}
Auth.dart 文件代码
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
class AuthService {
final FirebaseAuth _auth = FirebaseAuth.instance;
final FirebaseFirestore _firestore = FirebaseFirestore.instance;
//giriş yap fonksiyonu
Future<User?> signIn(String email, String password) async {
var user = await _auth.signInWithEmailAndPassword(
email: email, password: password);
return user.user;
}
//çıkış yap fonksiyonu
signOut() async {
return await _auth.signOut();
}
//kayıt ol fonksiyonu
Future<User?> createPerson(String name, String email, String password) async {
var user = await _auth.createUserWithEmailAndPassword(
email: email, password: password);
await _firestore
.collection("Person")
.doc(user.user!.uid)
.set({'userName': name, 'email': email});
return user.user;
}
}
解决方案
试试这种方式:
onTap:(){
_authService
.createPerson(_nameController.text, _emailController.text,
_passwordController.text)
.then((value) {
if(value !=null){
return Navigator.push(context,
MaterialPageRoute(builder: (context) =>LoginScreen()));
}
else{
debugPrint("user is not authenticated")
}
});
}
推荐阅读
- c++ - C ++无法从类中检索私有变量
- ios - WKWebView XMLHttpRequest 因自定义 url 方案而失败
- python - 即使从烧瓶中的数据库中删除,会话仍然可用
- typescript - 如何使用 Sequelize-Typescript 模型定义指定 NestJs 控制器的返回类型?
- node.js - 将multer的错误信息传递给其他路由器
- c++ - Visual Studio 控制台窗口的输入未显示
- reactjs - React:渲染作为对象属性的组件
- facebook-graph-api - 使用 manage_page 权限发布详细信息 api 调用
- firebase - 如何使用 geofirestore 库在地理查询中同时包含“near”和“where”方法
- jquery - 不正确的 Sequelize 查询