0

我想使用 Firesharp 插件从 Xamarin.Forms 中的 Firebase 数据库推送和获取配方数据。

我的模型类是配方类:

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>();
    }
}

所以将配方对象推送到 Firebase 数据库是可行的:

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

        client.Push("Recipes", recipe);

    }

Firebase 示例

但是当我想从数据库中获取数据时,我得到一个错误..

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;

    }

在这之后:

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

这个异常来了..

"{Newtonsoft.Json.JsonSerializationException: 无法将当前 JSON 对象(例如 {"name":"value"})反序列化为类型 'System.Collections.Generic.List`1[dignaBon_App.Models.Recipe]' 因为该类型需要一个 JSON 数组(例如 [1,2,3])来反序列化更正...}"

我不明白为什么我不能从数据库中取回数据..

有人能帮我吗?

4

1 回答 1

0

您需要使用您的 Firebase 秘密地址密钥来设置“配置”。为此,请转到您的 Firebase 控制台。

于 2019-05-12T09:13:07.483 回答