Question by
anish_aggarwal · Sep 29, 2021 at 03:22 PM ·
c#textdatabasegoogledata storage
How do I set a query value from firestore to a text field?
So I'm trying to get a document from firestore database, each document contains 2 fields "Country" and "Value", and I want to extract the document where Country is equal to the current location and from that document add the "Value" in the Text UI object
using UnityEngine;
using UnityEngine.UI;
using Firebase.Firestore;
using Firebase.Extensions;
using System;
public class GetDataFromDatabase : MonoBehaviour
{
[SerializeField] Text currentCountry;
[SerializeField] Text currentCountryValue;
FirebaseFirestore db;
private void Start()
{
db = FirebaseFirestore.DefaultInstance;
GetData();
}
void GetData()
{
string currCountry = currentCountry.text.ToString();
CollectionReference countries = db.Collection("Country");
Query query = countries.WhereEqualTo("Country", currCountry);
query.GetSnapshotAsync().ContinueWithOnMainThread((querySnapshotTask) => {
foreach (DocumentSnapshot documentSnapshot in querySnapshotTask.Result.Documents)
{
CountriesAndValues countriesAndValues = documentSnapshot.ConvertTo<CountriesAndValues>();
currentCountryValue.text = countriesAndValues.Value.ToString();
Debug.Log(currentCountryValue.text);
}
});
}
}
The CountriesAndValues is a struct with the following code:
using Firebase.Firestore;
[FirestoreData]
public struct CountriesAndValues
{
[FirestoreProperty]
public string Country { get; set; }
[FirestoreProperty]
public int Value { get; set; }
}
Comment