我正在尝试使用范围模型创建一个函数,该模型将有一个 favorite_border 图标,并且当它被按下时会更改为收藏夹图标。除此之外,增量计数器将向查看者显示来自 firebase 数据的点赞数。我正在尝试使用作用域模型来创建此功能,但我收到错误“断言失败:布尔表达式不得为空”。关于这个问题的任何想法?
class LikesModel extends Model {
DocumentSnapshot snapshot;
bool liked = false;
static LikesModel of(BuildContext context) =>
ScopedModel.of<LikesModel>(context);
bool isLiked() {
liked = true;
notifyListeners();
}
void pressed(){
liked = !liked;
notifyListeners();
}
void changeLikes() {
Firestore.instance
.collection(snapshot.documentID)
.document(snapshot.documentID)
.updateData({'likes': FieldValue.increment(liked ? 1 : -1)});
notifyListeners();
}
}
class LanchonetesContact extends StatefulWidget {
final DocumentSnapshot lanchonetes;
LanchonetesContact(this.lanchonetes);
@override
_LanchonetesContactState createState() => _LanchonetesContactState();
}
class _LanchonetesContactState extends State<LanchonetesContact> {
@override
Widget build(BuildContext context) {
return Padding(
padding: EdgeInsets.only(top: 0.0),
child: Card(
elevation: 1.0,
child: GestureDetector(
child: Container(
height: 70.0,
width: 390.0,
color: Colors.white,
child: Row(
children: <Widget>[
Icon(
LikesModel.of(context).isLiked() ?
Icons.favorite : Icons.favorite_border,
color: LikesModel.of(context).isLiked() ?
Colors.red : Colors.black,
size: 50.0,
),
StreamBuilder(
stream: Firestore.instance
.collection('lanchonetes')
.document(widget.lanchonetes.documentID)
.snapshots(),
builder: (context, snapshot) => Text(
snapshot.data.data["likes"].toString(),
style: TextStyle(fontSize: 40.0),
)
),
],
mainAxisAlignment: MainAxisAlignment.center,
),
),
onTap: () {
LikesModel.of(context).changeLikes();
LikesModel.of(context).pressed();
}
))
),