【问题标题】:Failed assertion: boolean expression must not be null with scoped model on flutter断言失败:布尔表达式不能为空,范围模型在颤振
【发布时间】:2020-04-23 08:56:30
【问题描述】:

我正在尝试使用范围模型创建一个函数,该模型将有一个 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();

                      }

                  ))


          ),

【问题讨论】:

  • 除了状态管理技术之外,您还可以只将变量存储在您的最父 Widget 中,并将变量和函数传递给子 Widget。
  • isLiked 应该检查是否喜欢 == bool,而不是将喜欢的内容更新为 true,如果您的目标是更新它,请将函数重命名为 updateLiked 或 setLiked更清晰
  • 您能告诉我们是哪一行出现错误吗?

标签: flutter scoped-model


【解决方案1】:

我不完全理解isLiked() 的目标,但它没有返回任何内容,预计会返回一个布尔值

bool isLiked() {

 liked = true;
 notifyListeners();

}

这会在下面的代码中引发错误,因为LikesModel.of(context).isLiked() 返回 null 并且条件运算符 (?:) 中的布尔值不能为 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() =&gt; liked;

甚至更干净

bool get isLiked =&gt; liked; //可能将liked设为私有会更好:_liked。

【讨论】:

  • 感谢 Jaime 成功了,这次检查正是我所缺少的。
【解决方案2】:

欢迎来到状态管理的核心概念,小部件的默认 setState 仅在释放小部件之前保持状态(在您的情况下发生在导航时。) 您需要有一个应用程序状态而不是小部件状态,读取各种状态管理技术之一,即 BLOC、Redux、Provider 等。 它们的作用是保持应用程序的状态,或者即使您处置小部件或导航时我应该说的任何小部件的状态。

【讨论】:

  • 谢谢@Yudhishthir,我正在尝试使用范围模型,我可以理解它的概念,但我仍然遇到困难。我用我的结果编辑了我的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-06
  • 2020-11-02
  • 1970-01-01
  • 2021-01-18
  • 2020-07-05
  • 2021-05-29
相关资源
最近更新 更多