问题
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