我已经完成了Google登录选项,但是当我无法检索已注册的Google帐户的详细信息时。
这是我的代码:
Future<String> signInWithGoogle() async {
String name;
String email;
String imageUrl;
final GoogleSignInAccount googleUser = await _googleSignIn.signIn();
final GoogleSignInAuthentication googleAuth = await googleUser.authentication;
final AuthCredential credential = GoogleAuthProvider.getCredential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
final FirebaseUser user = (await _auth.signInWithCredential(credential)).user;
print("signed in " + user.displayName);
return user;
// final FirebaseUser userDetails =
// (await _auth.signInWithCredential(credential)).user;
//// Add the following lines after getting the user
//// Checking if email and name is null
// assert(userDetails.email != null);
// assert(userDetails.displayName != null);
// assert(userDetails.photoUrl != null);
// name = userDetails.displayName;
// email = userDetails.email;
// imageUrl = userDetails.photoUrl;
//// Only taking the first part of the name, i.e., First Name
// if (name.contains(" ")) {
// name = name.substring(0, name.indexOf(" "));
// }
final GoogleSignInAccount googleSignInAccount = await googleSignIn.signIn();
final GoogleSignInAuthentication googleSignInAuthentication =
await googleSignInAccount.authentication;
final AuthCredential credential = GoogleAuthProvider.getCredential(
accessToken: googleSignInAuthentication.accessToken,
idToken: googleSignInAuthentication.idToken,
);
final AuthResult authResult = await _auth.signInWithCredential(credential);
final FirebaseUser user = authResult.user;
assert(!user.isAnonymous);
assert(await user.getIdToken() != null);
final FirebaseUser currentUser = await _auth.currentUser();
assert(user.uid == currentUser.uid);
return 'signInWithGoogle succeeded: $user';
}
答案 0 :(得分:1)
您代码中的变量credentials
未定义,应该像这样:
Future<FirebaseUser> _handleSignIn() async {
final GoogleSignInAccount googleUser = await _googleSignIn.signIn();
final GoogleSignInAuthentication googleAuth = await googleUser.authentication;
final AuthCredential credential = GoogleAuthProvider.getCredential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
final FirebaseUser user = (await _auth.signInWithCredential(credential)).user;
print("signed in " + user.displayName);
return user;
}
首先获取credentials
,然后将变量作为参数添加到signinwithcredential
尝试以下操作:
Future<String> signInWithGoogle() async {
String name;
String email;
String imageUrl;
// final FirebaseUser userDetails =
// (await _auth.signInWithCredential(credential)).user;
//// Add the following lines after getting the user
//// Checking if email and name is null
// assert(userDetails.email != null);
// assert(userDetails.displayName != null);
// assert(userDetails.photoUrl != null);
// name = userDetails.displayName;
// email = userDetails.email;
// imageUrl = userDetails.photoUrl;
//// Only taking the first part of the name, i.e., First Name
// if (name.contains(" ")) {
// name = name.substring(0, name.indexOf(" "));
// }
final GoogleSignInAccount googleSignInAccount = await googleSignIn.signIn();
final GoogleSignInAuthentication googleSignInAuthentication =
await googleSignInAccount.authentication;
final AuthCredential credential = GoogleAuthProvider.getCredential(
accessToken: googleSignInAuthentication.accessToken,
idToken: googleSignInAuthentication.idToken,
);
final AuthResult authResult = await _auth.signInWithCredential(credential);
final FirebaseUser user = authResult.user;
assert(!user.isAnonymous);
assert(await user.getIdToken() != null);
final FirebaseUser currentUser = await _auth.currentUser();
assert(user.uid == currentUser.uid);
return 'signInWithGoogle succeeded: $user';
}