'Why the value of a variable does not change when using initState in flutter
I have been trying to obtain the value of groupfav that is stored in FireStore to pass it as part of the reference to a Stream, but even when in initState I obtain the corresponding value, the original variable is not changed to the new value, therefore it does not load the list corresponding to the value obtained, I tried many solutions and ideas, but I can't make the String groupfav change to the new value.
static String? userID = FirebaseAuth.instance.currentUser?.uid;
static final userColeccion =
FirebaseFirestore.instance.collection("users");
String groupfav = ' '; //this value should be replaced by the future value obtained
late Stream<QuerySnapshot> task;
@override
void initState() {
super.initState();
userColeccion.doc("$userID").get().then((value) {
groupfav = value.data()!["groupfav"];
print(groupfav); //correctly prints the value I want (is "groupid4")
return groupfav;
});
taskGroup = FirebaseFirestore.instance
.collection("groups")
.doc(groupfav) // pass the obtained value
.collection("task")
.snapshots();
}
doing all this should replace String groupfav = ' '; to String groupfav = 'groupid4', but it doesn't work, it stays at ' ', thanks!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
