断言失败:布尔表达式在 flutter 上的作用域模型不能为 null
Failed assertion: boolean expression must not be null with scoped model on flutter
我正在尝试使用范围模型创建一个函数,该模型将有一个 favorite_border 图标,当按下它时会更改为最喜欢的图标。除此之外,一个增量计数器将向观众显示来自 firebase 数据的点赞数。我正在尝试使用范围模型来实现此功能,但出现错误 "Failed assertion: boolean expression must not be null"。关于这个问题的任何想法?
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();
}
))
),
欢迎来到状态管理的核心概念,小部件的默认 setState 仅在处理小部件之前保持状态(在您的案例中发生在导航时。)
您需要有一个应用程序状态而不是小部件状态,阅读各种状态管理技术之一,即 BLOC、Redux、Provider 等。
这些所做的是保持应用程序的状态,或者我应该说的任何小部件的状态,即使在您处理小部件或导航时也是如此。
我不完全理解 isLiked()
的目标,但它没有返回任何东西,预计会返回一个 bool
bool isLiked() {
liked = true;
notifyListeners();
}
这会在下面的代码中引发错误,因为 LikesModel.of(context).isLiked()
returns null 和 条件运算符 (?:) 中的 bool 不能为 null
Icon(
LikesModel.of(context).isLiked() ?
Icons.favorite : Icons.favorite_border,
color: LikesModel.of(context).isLiked() ?
Colors.red : Colors.black,
size: 50.0,
),
如果你只想检查 liked
你应该这样做
bool isLiked() => liked;
甚至更干净
bool get isLiked => liked;
//可能将 liked
设为私有会更好 :_liked.
我正在尝试使用范围模型创建一个函数,该模型将有一个 favorite_border 图标,当按下它时会更改为最喜欢的图标。除此之外,一个增量计数器将向观众显示来自 firebase 数据的点赞数。我正在尝试使用范围模型来实现此功能,但出现错误 "Failed assertion: boolean expression must not be null"。关于这个问题的任何想法?
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();
}
))
),
欢迎来到状态管理的核心概念,小部件的默认 setState 仅在处理小部件之前保持状态(在您的案例中发生在导航时。) 您需要有一个应用程序状态而不是小部件状态,阅读各种状态管理技术之一,即 BLOC、Redux、Provider 等。 这些所做的是保持应用程序的状态,或者我应该说的任何小部件的状态,即使在您处理小部件或导航时也是如此。
我不完全理解 isLiked()
的目标,但它没有返回任何东西,预计会返回一个 bool
bool isLiked() {
liked = true;
notifyListeners();
}
这会在下面的代码中引发错误,因为 LikesModel.of(context).isLiked()
returns null 和 条件运算符 (?:) 中的 bool 不能为 null
Icon(
LikesModel.of(context).isLiked() ?
Icons.favorite : Icons.favorite_border,
color: LikesModel.of(context).isLiked() ?
Colors.red : Colors.black,
size: 50.0,
),
如果你只想检查 liked
你应该这样做
bool isLiked() => liked;
甚至更干净
bool get isLiked => liked;
//可能将 liked
设为私有会更好 :_liked.