颤振 Firestore 文档路径空检查



嗨,我有一个从FireStore获取数据的FutureBuilder

有时我传递的文档路径为 null,因为这是一个非常大的数据库,并且删除了一些旧文档。

FutureBuilder(
future:
FirebaseFirestore.instance.doc(documentSnapshot['vid'].path).get(),
builder:
(BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
if (snapshot.hasError) {
return const Text("Something went wrong");
}
if (snapshot.hasData && !snapshot.data!.exists) {
return const Text("Document doesn't exist");
} else if (snapshot.connectionState == ConnectionState.done) {
Map<String, dynamic> data =
snapshot.data!.data() as Map<String, dynamic>;
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
data['name'],
style: GoogleFonts.poppins(
fontWeight: FontWeight.bold, fontSize: 14),
overflow: TextOverflow.ellipsis,
),
Text(
data['address'],
style: GoogleFonts.poppins(
fontWeight: FontWeight.w400,
fontSize: 11,
color: hexStringToColor('636567')),
overflow: TextOverflow.ellipsis,
maxLines: 1,
),
],
);
}
return const Text("Loading...");
},
)

documentSnapshot['vid].path有时是null.

我需要检查path是否null,只有当这不是null我才应该继续FutureBuilder.

我无法使用 if 条件检查这一点。

还有其他方法可以做到这一点吗?

由于您在继续FutureBuilder之前已经具有documentSnapshot['vid'].path的值,那么您可以在FutureBuilder之前检查它是否null

例:

documentSnapshot['vid'].path == null? Text('Not Found!') : FutureBuilder(...)

最新更新