我正在尝试在我的应用程序中添加 google_sign_in 功能,当我添加帐户详细信息时,它在控制台中显示错误,并且我没有重定向到另一个屏幕。
这是我的代码
class AuthMethods {
final FirebaseAuth auth = FirebaseAuth.instance;
getCurrentUser() async {
return auth.currentUser;
}
signInWithGoogle(BuildContext context) async {
final FirebaseAuth firebaseAuth = FirebaseAuth.instance;
final GoogleSignIn googleSignIn = GoogleSignIn();
final GoogleSignInAccount? googleSignInAccount =
await googleSignIn.signIn();
final GoogleSignInAuthentication? googleSignInAuthentication =
await googleSignInAccount?.authentication;
final AuthCredential credential = GoogleAuthProvider.credential(
idToken: googleSignInAuthentication?.idToken,
accessToken: googleSignInAuthentication?.accessToken);
UserCredential result = await firebaseAuth.signInWithCredential(credential);
User? userDetails = result.user;
if (result != null) {
Map<String, dynamic> userInfoMap = {
"email": userDetails!.email,
"name": userDetails.displayName,
"imgUrl": userDetails.photoURL,
"id": userDetails.uid
};
await DatabaseMethods()
.addUser(userDetails.uid, userInfoMap)
.then((value) {
Navigator.push(
context, MaterialPageRoute(builder: (context) => const GoogleUserInfo()));
});
}
}
}
class DatabaseMethods {
Future addUser(String userId, Map<String, dynamic> userInfoMap) {
return FirebaseFirestore.instance
.collection("User")
.doc(userId)
.set(userInfoMap);
}
}
我已尝试更新过时的依赖项,还尝试了 flutter clean 和 flutter pub get 命令,问题仍然存在。
这主要是因为您尚未重新启动模拟器或测试设备。
1. In your terminal, `flutter clean`
2. Go to your `pubspec.yaml`, and update all the dependencies to the latest version by `pub get` then `pub upgrade`.
3. If in VS Code, restart your emulator and VS code completely.
4. If in Android Studio, go to File> Invalidate Caches> Check true to all the values you see in dialog and let it restart.
5. Try again and it will run.