如何将数据写入实时数据库firebase flutter



我试图通过始终使用firebase(google_auth(首先登录用户来将数据写入实时数据库。但我的规则有问题。。。如果我设置"写和读";真";它们显然会去,如果我只是用";auth!=Null";但是当我使用这些规则时:

{
"rules": {
"users": {
"$uid": {
".read": "auth! = null && auth.uid == $ uid",
".write": "auth! = null && auth.uid == $ uid"
}
}
}
}

不再去了。因此,我认为问题在于auth.uid与uid不同。我附上了用于身份验证和数据提交的代码。

登录/注册:

Future <UserCredential> signInWithGoogle () async {
final GoogleSignInAccount? userGoogle = await GoogleSignIn (). signIn ();

final GoogleSignInAuthentication? Google user data =
await userGoogle? .authentication;

final credentialsGoogle = GoogleAuthProvider.credential (
accessToken: Google User data? .accessToken,
idToken: Google User data? .idToken,
);
return await FirebaseAuth.instance.signInWithCredential (Google credentials);
}

数据发送:

DatabaseReference ref = FirebaseDatabase.instance.ref ();
await ref.set ({
"users": {
"$uid": {
"name": "John",
}
}
});

uid是这样提取的:

checkLogin () {
FirebaseAuth.instance.authStateChanges (). Listen ((User? User) {
if (user! = null) {
username = user.displayName;
uid = user.uid;
}
});
}

我想是因为间距不对。。编辑了规则。请试试这个

{
"rules": {
"users": {
"$uid": {
".read": "auth != null && auth.uid == $uid",
".write": "auth != null && auth.uid == $uid"
}
}
}
}

相关内容

  • 没有找到相关文章

最新更新