简体   繁体   中英

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

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();" but it produced java.lang.NullPointerException error. Please can someone edit my code to propertly get database values? Here is "appDetails.java" class:

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

If your data is not saved in your Firbease database, then you need to change the rules like this:

{
  "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. First of all address can be null. Second the getID can be 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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