Future<bool> checkLiked(String questionID) async {
DocumentReference ref = questionBank.doc(questionID);
List<dynamic>? list;
await ref.get().then((value) {
list = value['userLiked'];
});
print(list!.contains(uid));
return list!.contains(uid);
}
it alawys return false also the uid already in the list and when i print the list it empty but inside the then method it contain the uid how to return the list outside the then method
CodePudding user response:
Maybe you're printing the list outside, but printing the value inside....
However both of them won't return the right values because you have to use .data()
to get the data from the result:
One way
Future<bool> checkLiked(String questionID) async {
DocumentReference ref = questionBank.doc(questionID);
List<dynamic>? list= (await ref.get()).data()?['userLiked'];
return list;
}
another way
Future<bool> checkLiked(String questionID) async {
DocumentReference ref = questionBank.doc(questionID);
List<dynamic>? list;
await ref.get().then((value) {
list = value.data()['userLiked'];
});
return list;
}
Also
Future<bool> checkLiked(String questionID) async {
DocumentReference ref = questionBank.doc(questionID);
List<dynamic>? list;
list = await ref.get().then((value) {
return value.data()['userLiked'];
});
return list;
}
And you can assign array to List<dynamic>
with no casting.
CodePudding user response:
Combining async
/await
and then
is a recipe for problems. You should use either async
/await
or then
, not both.
Future<bool> checkLiked(String questionID) async {
DocumentReference ref = questionBank.doc(questionID);
DocumentSnapshot value = await ref.get();
List<dynamic>? list = value['userLiked'];
print(list!.contains(uid));
return list!.contains(uid);
}