当我在 Flutter 上制作的应用程序中使用 google 登录时,电子邮件以空值返回到 firebase。我在运行时或登录时没有收到任何错误。我对 pubspec.yaml 文件上的 google_sign_in 版本进行了更改,但结果没有改变。
user_repository.dart :
import 'package:pisipisikolog/locator.dart';
import 'package:pisipisikolog/model/user_model.dart';
import 'package:pisipisikolog/services/auth_base.dart';
import 'package:pisipisikolog/services/fake_auth_service.dart';
import 'package:pisipisikolog/services/firebase_auth_service.dart';
import 'package:pisipisikolog/services/firestore_db_service.dart';
enum AppMode {DEBUG, RELEASE}
class UserRepository implements AuthBase{
FirebaseAuthService _firebaseAuthService = locator<FirebaseAuthService>();
FakeAuthenticationService _fakeAuthenticationService = locator<FakeAuthenticationService>();
FirestoreDbService _firestoreDbService = locator<FirestoreDbService>();
AppMode appMode = AppMode.RELEASE;
@override
Future<User> currentUser() async{
if (appMode == AppMode.DEBUG) {
return await _fakeAuthenticationService.currentUser();
} else {
return await _firebaseAuthService.currentUser();
}
}
@override
Future<User> signInAnonymously() async{
if (appMode == AppMode.DEBUG) {
return await _fakeAuthenticationService.signInAnonymously();
} else {
return await _firebaseAuthService.signInAnonymously();
}
}
@override
Future<bool> signOut() async{
if (appMode == AppMode.DEBUG){
return await _firebaseAuthService.signOut();
} else {
return await _firebaseAuthService.signOut();
}
}
@override
Future<User> signInWithGoogle() async{
if (appMode == AppMode.DEBUG) {
return await _fakeAuthenticationService.signInWithGoogle();
} else {
User _user = await _firebaseAuthService.signInWithGoogle();
bool _sonuc = await _firestoreDbService.saveUser(_user);
if (_sonuc) {
return _user;
}else return null;
}
}
@override
Future<User> signInWithFacebook() async{
if (appMode == AppMode.DEBUG) {
return await _fakeAuthenticationService.signInWithFacebook();
} else {
User _user = await _firebaseAuthService.signInWithFacebook();
bool _sonuc = await _firestoreDbService.saveUser(_user);
if (_sonuc) {
return _user;
}else return null;
}
}
@override
Future<User> createUserWithEmailandPassword(String email, String sifre) async{
if (appMode == AppMode.DEBUG) {
return await _fakeAuthenticationService.createUserWithEmailandPassword(email, sifre);
} else {
User _user = await _firebaseAuthService.createUserWithEmailandPassword(email, sifre);
bool _sonuc = await _firestoreDbService.saveUser(_user);
if (_sonuc) {
return _user;
}else return null;
}
}
@override
Future<User> signInWithEmailandPassword(String email, String sifre) async{
if (appMode == AppMode.DEBUG) {
return await _fakeAuthenticationService.signInWithEmailandPassword(email, sifre);
} else {
return await _firebaseAuthService.signInWithEmailandPassword(email, sifre);
}
}
}
用户模型.dart:
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
class User {
final String userID;
String email;
String userName;
String profilURL;
DateTime createdAt;
DateTime updatedAt;
int seviye;
User({@required this.userID, @required this.email});
Map<String, dynamic> toMap(){
return {
"UserID" : userID,
"email" : email,
"userName" : userName ?? '',
"profilURL" : profilURL ?? '',
"createdAt" : createdAt ?? FieldValue.serverTimestamp(),
"updatedAt" : updatedAt ?? FieldValue.serverTimestamp(),
"seviye" : seviye ?? 1,
};
}
User.fromMap(Map<String, dynamic> map):
userID = map['userID'],
email = map['email'],
userName = map['userName'],
profilURL = map['profilURL'],
createdAt = (map['createdAt'] as Timestamp).toDate(),
updatedAt = (map['updatedAt']as Timestamp).toDate(),
seviye = map['seviye'];
@override
String toString() {
return 'User{userID: $userID, email: $email, userName: $userName, profilURL: $profilURL, createdAt: $createdAt, updatedAt: $updatedAt, seviye: $seviye}';
}
}
截屏 https://i.stack.imgur.com/dolL0.jpg