我有来自以前或已弃用的 Flutter 版本的代码,用于将用户信息保存到 Firebase 数据库。 但我一直在尝试将代码升级到最新的 firebase_auth 包,但“FirebaseUser”被标记为错误。
void registerUser() async{
final FirebaseUser user = (await _auth.createUserWithEmailAndPassword(email: emailController.text, password: passwordController.text).catchError((ex){
//Check Error and display message
PlatformException thisEx = ex;
showSnackBar(thisEx.message);
})).user;
// check if user registration is successful
if(user != null){
DatabaseReference newUserRef = FirebaseDatabase.instance.reference().child('users/${user.uid}');
Map userMap = {
'fullname': fullNameController.text,
'email': emailController.text,
'phone': phoneController.text,
};
newUserRef.set(userMap);
Navigator.pushNamedAndRemoveUntil(context, MainPage.id, (route) => false);
}
}
我已尝试自行升级代码,但它的外观和行为与以前不同。这是我的代码,但我需要一些帮助。 提前致谢...
void registerUser() async{
//final UserCredential user = (await _auth.createUserWithEmailAndPassword(email: emailController.text, password: passwordController.text));
try {
UserCredential userCredential = await FirebaseAuth.instance
.createUserWithEmailAndPassword(
email: emailController.text,
password: passwordController.text,
);
if (userCredential != null) {
DatabaseReference newUserRef = FirebaseDatabase.instance.reference()
.child('users/${userCredential.toString}');
M
}
}
on FirebaseAuthException catch (e) {
if (e.code == 'weak-password') {
print('The password provided is too weak.');
} else if (e.code == 'email-already-in-use') {
print('The account already exists for that email.');
}
} catch (e) {
print(e);
}
}