Reputation: 11
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
}
Upvotes: 1
Views: 3988
Reputation: 1
May be you can try this
internal class DAOFood
{
FirebaseClient firebase = new FirebaseClient("YOUR FIREBASE URL");
public DAOFood()
{
}
public async Task<List<Food>> GetAllFoods()
{
List<Food> lstFood = (await firebase.
Child("Food").
OnceAsync<Food>()).Select(item => new Food
{
FoodId = item.Object.FoodId,
FoodName = item.Object.FoodName
}).ToList();
return lstFood;
}
}
Upvotes: 0
Reputation: 1679
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
}
}
}
}
Upvotes: 2