嗨,我有一个从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(...)