【发布时间】:2020-07-04 20:48:14
【问题描述】:
我有一个未来的 bool 方法,我想使用这个方法 IconButton 颜色。如果我使用以下代码,我会在我的设备屏幕上看到一条错误消息type'Future' is not a subtype of type 'bool' in type of cast.
Future<bool> ifExistInFavoriteList(String url) async {
bool ifExists = false;
SharedPreferences 首选项 = 等待 SharedPreferences.getInstance(); 列出我的 = (prefs.getStringList('myFavoriteList') ?? List());
my.contains(url) ? ifExists = true : ifExists = false;
如果存在则返回;
}
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
},
),
)
【问题讨论】: