[英]How to sign in with different types of user in flutter and firebase?
I am trying to sign in with different types of users, I have firebase authenticator to sign in and made a database with users and userType as a field, so anyone knows a function that shows different interface with different types of user?我正在尝试使用不同类型的用户登录,我有 firebase 身份验证器来登录并使用用户和 userType 作为字段创建了一个数据库,所以任何人都知道 function 显示不同类型的用户的不同界面? My Code:我的代码:
class _LoginScreenState extends State<LoginScreen> {
var email;
var password;
var username;
var gender;
var userType;
final usernameController = TextEditingController();
final emailController = TextEditingController();
final passwordController = TextEditingController();
final firestoreInstance = Firestore.instance;
void login() async {
final FirebaseAuth firebaseAuth = FirebaseAuth.instance;
firebaseAuth
.signInWithEmailAndPassword(
email: emailController.text, password: passwordController.text)
.then((result) {
{
Navigator.pushReplacementNamed(context, '/homepage');
}
}).catchError((err) {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text("Error"),
content: Text(err.message),
actions: [
FlatButton(
child: Text("Ok"),
onPressed: () {
Navigator.of(context).pop();
},
)
],
);
});
});
}
void getUserData() async {
try {
firestoreInstance
.collection('Users')
.document(usernameController.text)
.get()
.then((value) {
setState(() {
email = (value.data)['email'];
password = (value.data)['password'];
gender = (value.data)['gender'];
username = (value.data)['username'];
userType = (value.data)['userType'];
});
});
} catch (e) {
print(e.toString);
}
}
If you want to navigate to a different page after signing in, that'd be something like:如果您想在登录后导航到其他页面,则类似于:
final FirebaseAuth firebaseAuth = FirebaseAuth.instance;
firebaseAuth
.signInWithEmailAndPassword(
email: emailController.text, password: passwordController.text)
.then((result) {
firestoreInstance
.collection('Users')
.document(usernameController.text)
.get()
.then((value) {
var userType = (value.data)['userType'];
if (userType == "firstType") {
Navigator.pushReplacementNamed(context, '/homepage');
}
else if (userType == "secondType") {
Navigator.pushReplacementNamed(context, '/anotherpage');
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.