firebase - 有条件地从 ListView.builder 渲染小部件
问题描述
我的颤振电子商务应用程序已连接到 Cloud Firestore 后端。我有cart
几个文档的集合,products
其中包含用户添加的标题。每个产品都有一个字段order_status
,可能是in_cart
或者checked_out
值。现在我想在我的购物车屏幕上呈现它们,并根据order_status
. 我正在使用列表视图。Builder
方法,但由于存在itemCount
我无法呈现同一集合中的特定文档。这样做的正确方法是什么?我应该将产品存储在两个集合中吗?
代码:
ListView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: true,
itemCount: snapshot.data.length,
itemBuilder: (_, index) {
if (snapshot.data[index]['order_status'] == 'in_cart') {
_total = _total + snapshot.data[index]['total'];
return Container(
padding: EdgeInsets.only(bottom: 12.5),
child: Column(
children: [
Container(
height: (MediaQuery.of(context).size.height) / 7,
width: MediaQuery.of(context).size.width,
child: Container(
height: 105,
width: 85,
child: DecoratedBox(
decoration: BoxDecoration(
color: Colors.black12,
),
child: Container(
padding: EdgeInsets.all(10.0),
child: Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
crossAxisAlignment:
CrossAxisAlignment.center,
children: [
SizedBox(
width: 12.0,
),
IconButton(
icon: Image.network(
),
iconSize: 75.0,
onPressed: () {},
),
SizedBox(
width: 30.0,
),
Column(
children: [
Text(
snapshot.data[index]['title'],
style: kHeading1.copyWith(
fontSize: 22.5),
),
SizedBox(
height: 5.0,
),
Text(
'Rs.' +
snapshot.data[index]['price']
.toString() +
'/' +
snapshot.data[index]
['product_unit'],
style: kTitle1,
),
SizedBox(
height: 5.0,
),
Text(
'Total Rs.' +
snapshot.data[index]['total']
.toString(),
style: kTitle1,
),
],
),
SizedBox(
width: 20.0,
),
Row(
children: [
IconButton(
icon: Icon(
Icons.keyboard_arrow_up,
color: Colors.black,
),
onPressed: () {
increaseCart(
snapshot.data[index]
['title'],
snapshot.data[index]
['unit'],
snapshot.data[index]
['price']);
}),
Text(
snapshot.data[index]['unit']
.toString(),
style: kHeading2,
),
IconButton(
icon: Icon(
Icons.keyboard_arrow_down,
color: Colors.black),
onPressed: () {
decreaseCart(
snapshot.data[index]
['title'],
snapshot.data[index]
['unit'],
snapshot.data[index]
['price']);
}),
],
),
],
),
),
),
),
),
],
),
);
} else {
return null;
}
}),
解决方案
推荐阅读
- sql - 按 ID 计数分组记录并显示为每周输出的天/周
- intellij-idea - PhpStorm - 启动任务:无法选择 shellscript
- css - 我已经提到 flex 值为 0.5 但它不起作用
- haskell - 如何从另一个字符中“减去”一个字符?
- python - 当我尝试在 python 中创建浏览器时出现错误
- android - 在 kotlin 中单击按钮时更改 ViewModel 中的字符串值,然后将值存储在房间数据库中
- flutter - 使用真实设备和缺少默认通知通道时,Firebase FCM 声音不起作用
- javascript - 我如何无法在 digitalocean 中安装 puppeteer?
- mongodb - 为什么我得到一个错误找不到符号,符号:类 Mongoclient?
- logging - Serlog.Sinks.Graylog 没有将日志写入 graylog 仪表板。我在 dotnet 核心工作。我已经相应地设置了仪表板输入和流