更新时间:2023-11-27 19:42:04
根据您的用例,您可以使用 FutureBuilder,或者你保持简单,创建一个变量来处理逻辑:
Depending on your usecase you can either use a FutureBuilder, or you keep it simple and create a variable to deal with the logic:
在您的小部件内部:
bool isLiked = false;
从 initState() 中调用您的函数:
Call your function from within initState():
@override
void initState() {
super.initState();
checkIfLikedOrNot();
}
改变你的逻辑来修改你的变量并告诉 Flutter 重新渲染:
Change your logic to modify your variable and tell Flutter to rerender:
checkIfLikedOrNot() async{
DocumentSnapshot ds = await reference.collection("likes").document(currentUser.uid).get();
this.setState(() {
isLiked = ds.exists;
});
}
画出对应的Icon:
Icon(isLiked?Icons.favorite:FontAwesomeIcons.heart,
color: isLiked?Colors.red:null)
由于您显然正在处理值列表,因此将它们封装在 Object 中或使用 FutureBuilder 是有意义的.
Since you are apparently dealing with a list of values, it makes sense to encapsulate them in an Object or use the FutureBuilder.