首页 > 解决方案 > Flutter:访问 GridView Stream 元素内的未来值

问题描述

我有一个基于 Firestore 流的 Flutter GridView。对于 Grid 的每个元素,我希望通过访问 Firebase 文档使用 Future 值检查某些状态。代码如下

...

StreamBuilder<QuerySnapshot> {
  stream: db.collection('coll1').orderBy('field1').snapshots(),
  builder: : (context, snapshot) {
    if (!snapshot.hasData) {
      return Center(
        child: CircularProgressIndicator(),
      );
    } else {
      return GridView.count(
          crossAxisCount: 2,
          children: snapshot.data!.docs.map((doc) {
            //
            // here is where I would like the future value
            bool exists = checkIfExists(doc.id);
            // 
            // and use the value in the card below
            //
            return Card(
              child: ListTile(
                title: Text(doc.get('field1')),
                onTap: () {...},
              ),
            );
          }).toList());
        }
      },
   );
   .....

Future<bool> checkIfExists(id) async {
    ...
    DocumentSnapshot doc = await db.collection('coll2').doc(id).snapshot();
    return doc.exists ? true : false
}

谢谢

标签: fluttergoogle-cloud-firestore

解决方案


推荐阅读