I’m still new to DART but I thought this would be easy to accomplish.
I want to check if a List from a future contains an integer. My return type is list dynamic but when I do the check it’s telling me it’s a Future.
The function that returns a List dynamic
I want to change the color of an icon
Icon(
Icons.favorite,
color: checkIfLiked(), // Change color here using a future.
size: buttonSize,
);
Future<List> userLikes() async {
GetUserLikes userLikesClicks = GetUserLikes();
Future<List> userLikes = userLikesClicks.getLikes();
List userList = await userLikes;
return userList; // returns List<dynamic>
}
I want to check of the Array contains an integer and if it does to return a color.
Future<Color> checkIfLiked() async{
Color color;
List resultUsersLikes = await userLikes();
if (resultUsersLikes.contains(widget.id)) {
color = Colors.blue;
} else {
color = Colors.grey;
}
return color;
}
I have to return a Color, and I am already using a FutureBuilder on the widget.
I need the color to change the color of an icon.
This is the icon
Icon(
Icons.favorite,
color: checkIfLiked(),
size: buttonSize,
);
How can I add an async here to await the future?
3
Answers
as I see the
userLikes()
is aFuture
method, which means when you do this:you’re trying to call
contains()
which belongs to theList
type on aFuture
which didn’t resolve yet to return theuserList
from it.what you need to do is to wait until it gets it and resolves, then use its result ( which will be a
List
), like this:Now when you call
checkIfLiked()
, first it will get the response from theuserLikes()
then store it inresultUsersLikes
, then use it in the rest of your code :and when you wanna use
checkIfLiked()
in your other codes as theonPressed
of some button, you will need toawait
for it to get its final response, like this:You can set that color to the
Icon
, by calling this method oninitState
, then once it completes, update the state with theColor
, first declare aColor
variable in yourState
class:then set it to your
Icon
:then in your
initState
:now once the widget will be set inside the widget tree, the
checkIfLiked()
will get called, when it finishes, thethen
will be executed, which updates the state ofresultColor
with the color got from the method.Like your error message states, The method ‘contains’ isn’t defined for the type ‘Future’. You should first await the future to resolve, and then you can use the List it returns. First of all, your checkIfLiked() function must return Future and inside it you should await userLikes() function.
Since you don’t want
checkIfLiked()
to return aFuture<Color>
, you can try thisFirst declare a variable color and give it a default value.
Then call the below method
checkIfLiked()
when you need to.It will fetch the list of likes and finds the required color based on your logic.
Then it uses
setState
to change the variable color’s value from default value grey to required value.Color change will happen automatically in all your widgets, where
color
is referenced.