繁体   English   中英

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

[英]how do i write the data to Real-time Database firebase flutter?

我试图通过首先登录用户始终使用 firebase (google_auth) 将数据写入实时数据库。 但是我的规则有问题......如果我设置写入和读取“true”,它们显然是 go,如果我只使用“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"
      }
    }
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM