簡體   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