Can I use a variable as a property name in a Firebase Firestore .update() method? (Google Firebase & JavaScript)
Put the variable name in square brackets to have it evaluate to its value:
let name = 'NAME';
firebase.firestore.collection('test').doc('main').update({
[name]: 'text'
})
Firebase, variable as key name
For the latest version of Firebase, use brackets around the variable name:
firebase.database().ref("pathName").set({[variable] : 'MoreStuff'});
Using the other method of setting the index of the variable to the value will create an additional layer in the database structure.
How to create listener to custom variable inside document
This is not possible. With Cloud Function and Firestore an .onUpdate(
) is triggered when a document already exists and has any value changed (See https://firebase.google.com/docs/functions/firestore-events).
What you can do is to use the two snapshots that represent the data state before and after the triggering event and that are present in the change
object, as follows:
exports.updateUser = functions.firestore.document('Test/uhfL5NE199eYTGyfSH1srtrtee').onUpdate((change, context) => {
const newValue = change.after.data();
const previousValue = change.before.data();
//Check if the Score field has changed
if (newValue.Score !== previousValue.Score) {
//Score field has changed! -> Do whatever you want
} else {
//End the Cloud Function
return false;
}
});
Firebase field names are automatically changed?
I have Changed minifyEnabled
from true
to false
in build.gradle(module)
minifyEnabled: false
and That works for me...!
Why the value of a variable does not change when using initState in flutter
You're doing a synchronous call by attaching a callback to the usersCollection.get() call. It is only inside the .then that the asynchronous call gets fulfilled. This call does not wait and it just continues right through to the next call, which is fetching the FirebaseFiresstore.instance, by then the value is still empty for the groupfav. What you need to do is nest that call inside the .then(), because it is then (pun intended!) when the value will be set because of how asynchronous calls work when using the .then chain of events, so do it like this:
userColeccion.doc("$userID").get().then((value) {
groupfav = value.data()!["groupfav"];
print(groupfav); //correctly prints the value I want (is "groupid4")
// right inside the **.then, after getting the value
// THEN you can fetch the document with name 'groupfav'
// from your collection
taskGroup = FirebaseFirestore.instance
.collection("groups")
.doc(groupfav) // pass the obtained value
.collection("task")
.snapshots();
});
Unfortunately you cannot make the initState asynchronous (decorating it with the async keyword), otherwise I would've suggested using the await instead of the .then. Another way would be to take all these oeprations into a separate method which indeed waits for the call on the usersCollection, as in:
@override
void initState() {
super.initState();
getGroupFavData();
}
void getGroupFavData() async {
var groupFavData = await userColeccion.doc("$userID").get();
var groupfav = groupFavData.data()!['groupfav'];
taskGroup = FirebaseFirestore.instance
.collection("groups")
.doc(groupfav) // pass the obtained value
.collection("task")
.snapshots();
}
Related Topics
How to Change the Background Color of Action Bar'S Option Menu in Android 4.2
How to Layout Text to Flow Around an Image
How to Read a Text File from the Sd Card in Android
Error Inflating Class Android.Support.Design.Widget.Navigationview
How to Pass a Value from One Activity to Another in Android
Disable Scrolling in All Mobile Devices
Android Deprecated Apache Module (Httpclient, Httpresponse, etc.)
Wait Firebase Async Retrieve Data in Android
Difference and Uses of Oncreate(), Oncreateview() and Onactivitycreated() in Fragments
How to Check If Location Services Are Enabled
Android Material and Appcompat Manifest Merger Failed
Using Facebook Sdk in Android Studio
Soft Keyboard Open and Close Listener in an Activity in Android
Playing an Arbitrary Tone With Android