首页 > 解决方案 > 在颤振中添加if条件后,列表视图卡不起作用

问题描述

我的列表视图卡没有返回特定用户存储的所有项目,我想获取用户保存的所有产品

 body: (products != null)
        ? ListView.builder(
            itemCount: products.length,
            itemBuilder: (context, index) {
              if (products[index].ownerId == user.uid) {
                return Card(
                  child: ListTile(
                    leading: Image.asset('graphics/broccoli.png'),
                    title: Text(products[index].name),
                    subtitle:
                        Text(products[index].price.toString() + " Rs"),
                    trailing: Icon(Icons.more_vert),
                  ),
                );
              } else {
                return null;
              }
            })
        : Center(child: CircularProgressIndicator()));

标签: firebaseflutterlistviewflutter-listview

解决方案


在该else部分中,返回 aSizedBox()而不是null

} else {
  return const SizedBox();
}

这是因为任何小部件的构建方法或构建器函数必须始终返回 a Widget,并且 anull不是 a Widget

因此,每当我们想显示“无”或“空小部件”时,我们可以简单地返回一个SizedBox().


推荐阅读