var result;
void searching() async{
var searchData = await FirebaseFirestore.instance.collection('users').where('email',isEqualTo: userEmail).get();
setState(() {
result = searchData.docs.first;
});
}
void addFollowing(var username , var email , var phoneNumber) async{
searching();
await FirebaseFirestore.instance.collection('users').doc(result).collection('following').doc().set({
'username':username,
'email':email,
'phoneNumber':phoneNumber
});
print(result);
}
I have two function searching and addfollowing . The searching function returns a document that matches the given email and sets the id of that document in result . But it returns null for some reason when i call that fucntion in the addfollowing function .
I am using the add following function on an onTap of a gesturedetector. The first press returns null but the second press returns the data .
2
Answers
the searching is not complete then yet. you need to await it. So change
to
Also, I believe you need to change this
to this as well
You should await the searching function in addFollowing to ensure that it completes before you proceed to use the result.