【问题标题】:Using Firesharp in Xamarin.Forms to get Data from Firebase Database在 Xamarin.Forms 中使用 Firesharp 从 Firebase 数据库中获取数据
【发布时间】:2018-03-31 22:22:04
【问题描述】:

我想使用 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 Example

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

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])来反序列化 correc...}"

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

有人可以帮助我吗?

【问题讨论】:

  • 您的查询返回一个对象,但您正试图反序列化为一个数组。尝试反序列化为配方而不是列表
  • 响应包含所有食谱对象的 json,但他无法反序列化它.. 我必须有一个 List 因为我想要数据库中的所有食谱.. 有什么建议吗?跨度>
  • Firebase 返回的实际 json 是什么样的?

标签: firebase firebase-realtime-database xamarin.forms fire-sharp


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-22
    相关资源
    最近更新 更多