我有一个返回布尔值的Future方法,并且想将这个方法的返回值用作IconButton的颜色。如果我使用以下代码,我的设备屏幕上会显示错误信息'type'Future'在类型转换中不是'bool'类型的子类'
Future<bool> ifExistInFavoriteList(String url) async {
bool ifExists = false;
SharedPreferences prefs = await SharedPreferences.getInstance(); List my = (prefs.getStringList('myFavoriteList') ?? List());
if (my.contains(url)) { ifExists = true; } else { ifExists = false; }
return ifExists;
}
bool _isLiked() {
bool a = false;
a = ifExistInFavoriteList(widget.imageUrl) as bool;
return a;
} }
Expanded(
child: IconButton(
color:
_isLiked() ? Colors.deepPurple : Colors.green,
icon: Icon(Icons.category),
onPressed: () {
//TO-DO
},
),
)