Reputation: 309
I have a button, and when I press it I want it to update a null value with a string. In the code, you will notice a future builder, and just know I need that to determine what I should exactly update depending on the data status in Firestore. When a user is created the value of isPledged
is set to null, but as you see by the if statement if that value is null, I want to update it with a string "true". However, when I do so, nothing changes in Firestore with the value remaning null. I'm a bit confused about where I messed up so any help would be much appreciated. Also, I know I could remove the if statement but I'm going to add another option if the value does have data, so that's why that's there.
onPressed: () {
FutureBuilder(
future: getPledgedStatus(),
builder: (_, AsyncSnapshot snapshot) {
if (!snapshot.hasData) {
final CollectionReference
users = FirebaseFirestore.instance.collection('UserNames');
FirebaseAuth auth = FirebaseAuth.instance;
String uid = auth.currentUser.uid.toString();
users.doc(uid).update(
{'isPledged': "true"}
);
}
},
);
Navigator.of(context).pop();
},
Upvotes: 0
Views: 235
Reputation: 309
Okay so after Muthu Thavamani reminded me that I could simply use the async function instead of a future builder it worked, here's the new code:
onPressed: () async {
try {
final CollectionReference users = firestore.collection('UserNames');
final String uid = auth.currentUser.uid;
final result = await users.doc(uid).get();
var isPledged = result.data()['isPledged'];
if (isPledged == null) {
FirebaseFirestore.instance.collection('UserNames').doc(uid).update({
"isPledged": "true",
});
Navigator.of(context).pop();
} else {
//Something else
}
} catch (e) {
print(e);
}
}
Upvotes: 2