How to Get Value from Firebase in Xamarin?

拟墨画扇 提交于 2020-01-01 19:35:15

问题


How to Get Data from Firebase Realtime Database using Xamarin Firebase.Database Android, as there is no documentation available and I am new user.

public void GetSubjects()
{
   Database.Reference.Child("childName").Ref.AddListenerForSingleValueEvent(new ValueEventListener());
}

public class ValueEventListener : Java.Lang.Object, Firebase.Database.IValueEventListener
{

    public void OnCancelled( DatabaseError error )
    {
       throw new NotImplementedException();
    }

    public void OnDataChange( DataSnapshot snapshot )
    {

      //How to Get Value from Snapshot?
      ClassName a = snapshot.GetValue(ClassName) //Gives Error

    }

回答1:


Look at this and see if it works...

private void GetData()
{
    FirebaseDatabase
        .Instance
        .Reference
        .Child("Put your child node name here")
        .AddListenerForSingleValueEvent(new DataValueEventListener());
}


class DataValueEventListener: Java.Lang.Object, IValueEventListener
{
    public void OnCancelled(DatabaseError error)
    {
        // Handle error however you have to
    }

    public void OnDataChange(DataSnapshot snapshot)
    {
        if (snapshot.Exists())
        {
            DataModelClass model = new DataModelClass();
            var obj = snapshot.Children;

            foreach (DataSnapshot snapshotChild in obj.ToEnumerable())
            {
                if (snapshotChild.GetValue(true) == null) continue;

                model.PropertyName = snapshotChild.Child("Put your firebase attribute name here")?.GetValue(true)?.ToString();
                model.PropertyName = snapshotChild.Child("Put your firebase attribute name here")?.GetValue(true)?.ToString();

                // Use type conversions as required. I have used string properties only
            }
        }
    }
}


来源:https://stackoverflow.com/questions/43500686/how-to-get-value-from-firebase-in-xamarin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!