I just want to return the value of each condition and use it as the value of the function.
Here is my code for it.
var getStuff = chechIfExisting();
chechIfExisting() async {
var isExisting = await FirebaseFirestore.instance
.collection('merchants')
.doc(userUid)
.get();
if (isExisting.exists) {
return 'exists';
}
if (!isExisting.exists) {
return 'nope';
} else {
return 'error';
}
}
and I am not using any Stateless and Stateful Widget since this file contains only the widgets such as appbar/mydrawer. I wanted to use the 'getStuff' variable in a if statement under the myDrawer Widget, since I want to dynamically check if the the data that I am fetching is existing or not.
myDrawer(BuildContext context) {
print('getStuff');
// only prints 'Instance of 'Future<String>' and does not return the value.
}
I want to be able to use the 'getStuff' variable as
myDrawer(BuildContext context) {
if(getStuff == 'exists'){
// code here
}
Any helps/ideas on how to solve this are appreciated!
CodePudding user response:
with this line:
var getStuff = chechIfExisting();
You're not waiting for the method to finishes executing, since it's a Future
, not using either await
/async
or then
to resolve values after the Future
finishes will get you that getStuff()
is a type of Instance of 'Future<String>
, before the running of the myDrawer
function, you need either to:
var getStuff = await chechIfExisting(); // inside an async method
or:
chechIfExisting().then((resolvedValue) {
getStuff = resolvedValue;
});
then you can run the myDrawer
method and get it working fine