当我尝试使用firebaseauth登录时,我总是收到错误提示
我一直在网上关注有关如何执行此操作的教程。但这似乎仍然对我不起作用。
这是我的代码:
import 'package:firebase_auth/firebase_auth.dart';
import 'package:google_sign_in/google_sign_in.dart';
bool isAuth = false;
FirebaseAuth _auth = FirebaseAuth.instance;
final googleSignIn = GoogleSignIn();
Future<bool> signUp({String email, String password}) async {
try {
User user = (await _auth.createUserWithEmailAndPassword(email: email, password: password)).user;
return Future.value(true);
} catch (e) {
switch (e.code) {
case "email-already-in-use":
print("email-already-in-use");
}
}
}
Future<bool> signIn({String email, String password}) async {
try {
User user = (await _auth.signInWithEmailAndPassword(email: email, password: password)).user;
return Future.value(true);
} catch (e) {
switch (e.code) {
case "user-not-found":
print("user-not-found");
break;
}
}
}
Future<bool> signOutUser() async {
User user = await _auth.currentUser;
if (user.providerData[1].providerId == "google.com") {
await googleSignIn.signOut();
}
_auth.signOut();
return Future.value(true);
}
答案 0 :(得分:0)
这样定义您的try catch块:
try {
User user = (await _auth.createUserWithEmailAndPassword(email: email, password: password)).user;
return Future.value(true);
} on FirebaseAuthException catch (e) {
switch (e.code) {
case "email-already-in-use":
print("email-already-in-use");
return null;
}
}
}
和
try {
User user = (await _auth.signInWithEmailAndPassword(email: email, password: password)).user;
return Future.value(true);
}on FirebaseAuthException catch (e) {
switch (e.code) {
case "user-not-found":
print("user-not-found");
break;
return null;
}
}