I'm trying to get the value from an Async function that returns an integer which is then used to set the color of a widget in my UI.
Async function:
// Returns a future<int> instead of an int due to async
Future<int> getLikeStatus(String name) async {
int likeStatus =
await getLikeStatusFromPostLikes(name); // this returns an int
return likeStatus;
}
Post Function:
Future <List<dynamic>> fetchData() async {
// Some code to make GET request to private server with data and push as posts
final response = await posts.get();
final result = response.map((m) => Post.fromJson(m)).toList();
return result;
}
Downstream usage - uses the above post function:
// snapshot is populated by post future in FutureBuilder
child: FutureBuilder<List<dynamic>>(
future: post,
builder: (context, snapshot) {
if (snapshot.hasData) {
...
Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
InkWell(
child: IconButton(
icon: Icon(Icons.thumb_up),
color: getLikeStatus(snapshot.data[index].name) == 1
? Colors.green
: Colors.blue) // cannot use future here
),
],
);
How can I return the likeStatus variable to use for the color attribute in my widget?
EDIT: Added code showing use of FutureBuilder
postvariable?