Home > Software design >  get the value from nested node firebase databse
get the value from nested node firebase databse

Time:09-05

I'm trying to get all the value of Nutrients node, but it return null.

this is my database :

enter image description here

I have recipe node with values, then i have nested child "Nutrients" i want to get all the values of it

data_ref_recipe = FirebaseDatabase.getInstance().getReference("Recipes");
data_ref_recipe.child(recipe_id).addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
         recipe_name = String.valueOf(snapshot.child("recipe_name").getValue());
        ingrediants = String.valueOf((snapshot.child("ingrediants").getValue()));
        instructions = String.valueOf((snapshot.child("instructions").getValue()));

        carb = String.valueOf(snapshot.child("Nutrients").child("carb").getValue());
        fat = String.valueOf(snapshot.child("Nutrients").child("fat").getValue());
        protien = String.valueOf(snapshot.child("Nutrients").child("protine").getValue());
        calories = String.valueOf(snapshot.child("Nutrients").child("calories").getValue());

        rcarb.setText(carb);
        rfat.setText(fat);
        rprotien.setText(protien);
        rcalories.setText(calories);
    public void onCancelled(@NonNull DatabaseError error) {

    }
});

I get all the values of ricpe. but i get null for Nutrients.

This is my recipe class :

public class Recipe implements Serializable {

String recipe_id,uid, recipe_name , ingrediants ,instructions ,recipe_type , cook_time, quantity, date;
Uri recipe_image ;

Double carb,fat, protine, calories;

//for firebase
public Recipe (){}

public Recipe (Double carb, Double fat, Double protine, Double calories){
    this.carb = carb;
    this.fat = fat;
    this.protine = protine;
    this.calories = calories;
}

public Recipe(String recipe_id,String uid, String recipe_name, String ingrediants, String instructions, String recipe_type, String cook_time, String quantity , String date) {
    this.recipe_id = recipe_id;
    this.uid = uid;
    this.recipe_name = recipe_name;
    this.ingrediants = ingrediants;
    this.instructions = instructions;
    this.recipe_type = recipe_type;
    this.cook_time = cook_time;
    this.quantity = quantity;
    this.date = date ;
}

CodePudding user response:

There's an additional colon (:) in the Nutrients: key, which you'll also need to include in the code:

DataSnapshot nutrients = snapshot.child("Nutrients:");
carb = String.valueOf(nutrients.child("carb").getValue());
fat = String.valueOf(nutrients.child("fat").getValue());
protien = String.valueOf(nutrients.child("protine").getValue());
calories = String.valueOf(nutrients.child("calories").getValue());

If this still doesn't work, check the exact spelling and spaces of the node name in the console.


Also (not the problem here, but in general) you should never leave onCancelled empty, as it means you're ignoring possible errors. At its minimum, it should be:

public void onCancelled(@NonNull DatabaseError databaseError) { 
    throw databaseError.toException(); 
}
  • Related