StreamBuilder 快照是否为空

时间:2021-05-19 16:22:30

标签: flutter google-cloud-firestore flutter-layout

StreamBuilder(
        stream: firestore
            .collection('Product')
            .doc(order.productId)
            .collection('AbleToReview')
            .doc(auth.currentUser.uid)
            .snapshots(),
        builder: (context, snapshot) {
          if (snapshot.hasError) {
            return Text('Something went wrong');
          }

          if (snapshot.connectionState == ConnectionState.waiting) {
            return Center(
              child: CircularProgressIndicator(),
            );
          }

          if (snapshot.data==null){
            return Text('no data');
          } else {
            return Text('have data');
          }
        }
    )

有时firestore.collection('Product').doc(order.productId).collection('AbleToReview').doc(auth.currentUser.uid).snapshots() 查询。由于数据存在与否,我尝试重新调整不同的小部件。但是 snapshot.data==null!snapshot.hasData 不是我想要的正确方式。

1 个答案:

答案 0 :(得分:0)

您可以使用 snapshot.data.documents.isEmpty() 检查查询中的实际数据是否为空。

您可以将代码更新为以下代码:

    if (snapshot.data==null || snapshot.data.data().isEmpty()){
        return Text('no data');
    } else {
        return Text('have data');
    }