0

I want to push and get Recipe-Data from my Firebase Database in Xamarin.Forms with the Firesharp Plugin.

My Model Class is the Recipe Class:

public class Recipe
{
    public string title { get; set; }

    public string workTime { get; set; }

    public string degreeOfDifficulty { get; set; }

    public string caloriesPerPerson { get; set; }

    public string preparation { get; set; }

    public string cookingBakingTime { get; set; }

    public string restTime { get; set; }

    public int portions { get; set; }

    public string pictureSource { get; set; }

    public List<Ingredient> ingredients { get; set; }


    public Recipe()
    {
        ingredients = new List<Ingredient>();
    }
}

So Push Recipe-Objects to the Firebase DB works:

 public async Task PushRecipe(Recipe recipe)
    {
        IFirebaseClient client = new FirebaseClient(config);

        client.Push("Recipes", recipe);

    }

Firebase Example

But when i want to get Data from the Database i get an Error..

public async Task<List<Recipe>> GetAllRecipes()
    {
        IFirebaseClient client = new FirebaseClient(config);

        FirebaseResponse response = await client.GetAsync("Recipes");

        try
        {

            List<Recipe> recipelist = response.ResultAs<List<Recipe>>();

            return recipelist;

        } catch (Exception e){

        }

        return null;

    }

After this:

List<Recipe> recipelist = response.ResultAs<List<Recipe>>();

this Exception comes..

"{Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[dignaBon_App.Models.Recipe]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correc…}"

I don´t understand why i can´t get Data from the Database back..

Can someone help me?

Dahl93
  • 1
  • 2
  • your query returns a single object, but you are trying to deserialize into an array. Try deserializing into a Recipe instead of a List< Recipe> – Jason Apr 01 '18 at 03:35
  • The response contains a json of all Recipe Objects, but he cant deserialize it.. i must have a List because i want all Recipes which are in the Database.. any suggestions? – Dahl93 Apr 01 '18 at 08:46
  • what does the actual json returned by Firebase look like? – Jason Apr 01 '18 at 14:56

1 Answers1

0

You need instatiate the "config" with your firebase secret address key. Go to your Firebase console for this.

Ideas
  • 1