'How to convert Firebase arrays to C# objects in Xamarin?

I have found many answers in Java but not specifically in C#.

Currently, I have the following data model in my Firestore cloud database:

enter image description here

I then layered out my models in C#:

public class User
{
    public string Uid { get; set; }
    public string Username { get; set; }
    public List<TaskDetails> Tasks { get; set; }
}

public class TaskDetails
{
    public DateTime DueDate { get; set; }
    public bool IsCompleted { get; set; }
    public string Details { get; set; }
}

Here is how I get the data so far:

public class FirestoreUsers : Java.Lang.Object, IFirestoreUsers, IOnCompleteListener
{
    List<User> Users = new List<User>();

    public async Task<IList<User>> GetUsers()
    {
        FirebaseFirestore.Instance.Collection("Users").Get().AddOnCompleteListener(this);

        await System.Threading.Tasks.Task.Delay(1000);

        return Users;
    }

    public void OnComplete(Android.Gms.Tasks.Task task)
    {
        var snapshot = (QuerySnapshot)task.Result;

        if (snapshot.IsEmpty) return;

        var document = snapshot.Documents;
        Users.Clear();

        foreach (DocumentSnapshot user in document)
        {
            User userModel = new User
            {
                Uid = user.Id, // 'Document Id' is the 'User' 'Uid'
                Username = user.Get("Username"),
            };

            Users.Add(userModel);
        }
    }
}

At the moment, I can only get the Uid and Username. Now, I need to get the Tasks.. struggled for hours and can only find answer in Java. I am not sure where to get started.

What I tried

I looked into this answer:

https://stackoverflow.com/a/70122197/12485722

But it seems like he's using a different library than me. I use Xamarin.Firebase.Firestore.



Solution 1:[1]

There are several options.

Option one:

databaseReference.child("category").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
           for (int i=0;i<3;i++) {
             // category is an ArrayList you can declare above
                category.add(dataSnapshot.child(String.valueOf(i)).getValue(String.class));
        }
        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) { // Do something for this
        }
    });
}

Option two:

public class FirestoreUsers : Java.Lang.Object, IFirestoreUsers, IOnCompleteListener
{
    List<User> Users = new List<User>();
    public async Task<IList<User>> GetUsers()
    {
        FirebaseFirestore.Instance.Collection("Users").Get().AddOnCompleteListener(this);
        await System.Threading.Tasks.Task.Delay(1000);
        return Users;
    }
    public void OnComplete(Android.Gms.Tasks.Task task)
    {
        var snapshot = (QuerySnapshot)task.Result;
        if (snapshot.IsEmpty) return;
        var document = snapshot.Documents;
        Users.Clear();
        foreach (DocumentSnapshot user in document)
        {
            User userModel = new User
            {
                Uid = user.Id, // 'Document Id' is the 'User' 'Uid'
                Username = user.Get("Username"),
            };
            Users.Add(userModel);
        }
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 qqNade