我在向firestore 'users'集合添加经过身份验证的用户时卡住了。
Unhandled Exception: [cloud_firestore/not-found] Some requested document
was not found.
用户通过Google登录:
class GoogleSignInProvider extends ChangeNotifier {
final GoogleSignIn _googleSignIn = GoogleSignIn();
GoogleSignInAccount _user;
GoogleSignInAccount get user => _user;
AuthService auth = AuthService();
Future googleSignIn(BuildContext context) async {
try {
final googleUser = await _googleSignIn.signIn();
if (googleUser == null) return;
_user = googleUser;
final googleAuth = await googleUser.authentication;
final AuthCredential credential = GoogleAuthProvider.credential(
idToken: googleAuth.idToken, accessToken: googleAuth.accessToken);
await FirebaseAuth.instance.signInWithCredential(credential);
} catch (e) {
print(e.toString());
}
final User currentUser = FirebaseAuth.instance.currentUser;
if (currentUser != null)
usersRef.add(currentUser.uid);
Navigator.of(context).pushReplacement(
CupertinoPageRoute(builder: (_) => TabScreen()));
notifyListeners();
}
但是,无论我如何尝试验证firebase id都不会添加到usersRef (firestore集合)中。我怎么修理它?
我的firestore规则是:
match /users/{userId}/{documents=**} {
allow read: if request.auth.uid != null;
allow write, update, create, delete: if isOwner(userId);
}
function isOwner(userId) {
return request.auth.uid == userId;
}
帮助非常感谢!
所以这解决了我的问题:
final User currentUser = FirebaseAuth.instance.currentUser;
if (currentUser != null)
await usersRef.doc(currentUser.uid).set({'email':
user.email, 'username': user.displayName, 'photoUrl':
user.photoURL});
我想,我想多了。
谢谢ppl的指导和帮助!
照做
Map data = {};
FirebaseFirestore.instance
.collection('usersCollection')
.doc(currentUser.uid)
.set(data);
代替usersRef.add(currentUser.uid);
当您有doc id时使用set
,当您想要自动生成id时使用add
。