简体   繁体   English

访问Firebase实时数据库值

[英]Access Firebase realtime database values

I'm making app for my developers team and I want to add app version editor for all logged in users with firebase realtime database, I use code 我正在为我的开发人员团队制作应用程序,我想为所有具有Firebase实时数据库的登录用户添加应用程序版本编辑器,我使用代码

appIdEndPoint = mDatabase.child("users").child(user.getUid()).child("app").child("id");
        appVersionEndPoint = mDatabase.child("users").child(user.getUid()).child("app").child("latest_version");
        uid_text.setText("User firebase ID: " + user.getUid());

        appIdEndPoint.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                appDetails address = dataSnapshot.getValue(appDetails.class);
                if (address.getID().equals("")){
                    appIdEndPoint.setValue(getRandom(1000, 9999));
                }
                appid_text.setText("App ID: " + address.getID());
                appid_edittext.setText(address.getID());
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Log.d("AppID", databaseError.getMessage());
                Toast.makeText(AppDetailsEditor.this, "Failed to fetch data from database", Toast.LENGTH_LONG);
                finish();
            }
        });

        appVersionEndPoint.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                appDetails address = dataSnapshot.getValue(appDetails.class);
                if (address.getVersion().equals("")){
                    appVersionEndPoint.setValue("Waiting for developers input");
                }
                appversion_text.setText("App version: " + address.getVersion());
                appversion_edittext.setText(address.getVersion());
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Log.d("AppVersion", databaseError.getMessage());
                Toast.makeText(AppDetailsEditor.this, "Failed to fetch data from database", Toast.LENGTH_LONG);
                finish();
            }
        });

I tried to use "String appID = dataSnapshot.getValue().toString();" 我尝试使用“字符串appID = dataSnapshot.getValue()。toString();” but it produced java.lang.NullPointerException error. 但是它产生了java.lang.NullPointerException错误。 Please can someone edit my code to propertly get database values? 请有人可以编辑我的代码以正确获取数据库值吗? Here is "appDetails.java" class: 这是“ appDetails.java”类:

public class appDetails {

public String ID = "";
public String version = "";

public appDetails(){

}

public appDetails(String ID, String version){
    this.ID = ID;
    this.version = version;
}

public String getID (){
    return ID;
}
public String getVersion (){
    return version;
}

} }

EDIT: 编辑:

Code showed above too produce java.lang.NullPointerException error 上面显示的代码也产生java.lang.NullPointerException错误

If your data is not saved in your Firbease database, then you need to change the rules like this: 如果您的数据未保存在Firbease数据库中,则需要更改如下规则:

{
  "rules": {
    ".read": true,
    ".write": true
  }
}

But change your rules to this just temporarily, please don't leave it that way. 但是只是暂时更改规则,请不要那样做。

Your code is not null safe. 您的代码不是null安全的。 First of all address can be null. 首先地址可以为空。 Second the getID can be null. 其次,getID可以为null。 Anyway this is better: 无论如何这是更好的:

if (address.getID() == null || "".equals(address.getID())){
    appIdEndPoint.setValue(getRandom(1000, 9999));
}

Since your database is currently empty, the ValueListener.onDataChange() will fire with an empty snapshot. 由于您的数据库当前为空,因此ValueListener.onDataChange()将使用空快照启动。 Your code needs to check for this: 您的代码需要检查以下内容:

appIdEndPoint.addValueEventListener(new ValueEventListener() {
  @Override
  public void onDataChange(DataSnapshot dataSnapshot) {
    if (dataSnapshot.exists()) {
      appDetails address = dataSnapshot.getValue(appDetails.class);
      ...

You'll need such a check in any ValueEventListener that listens for data that might not exist. 您需要在任何ValueEventListener中进行此类检查,以侦听可能不存在的数据。

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

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