【发布时间】:2021-07-12 08:06:49
【问题描述】:
我只想在 Flutter 中显示 Firestore 中的特定文档。为此,我尝试使用if 语句,但我只收到一个没有数据的空白屏幕。如果我删除if 语句,那么我将显示所有文档。
【问题讨论】:
标签: flutter google-cloud-firestore flutter-layout
我只想在 Flutter 中显示 Firestore 中的特定文档。为此,我尝试使用if 语句,但我只收到一个没有数据的空白屏幕。如果我删除if 语句,那么我将显示所有文档。
【问题讨论】:
标签: flutter google-cloud-firestore flutter-layout
除了获取整个集合之外,您还可以通过此获取特定文档。在这种情况下,无需添加 if 条件。请参阅下面的代码。
StreamBuilder(
stream: FirebaseFirestore.instance.collection("<collectionPath>").doc("<docId>").snapshots(),
builder: (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
return ListView();
},
);
我刚试过,下面的代码在我的情况下工作。
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: "test",
home: Scaffold(
body: StreamBuilder(
stream: FirebaseFirestore.instance
.collection("<collection>")
.doc("<docId>")
.snapshots(),
builder:
(BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
return Center(
child: Text(snapshot.data.data()["fieldName"]),
);
},
),
),
);
}
}
就我而言,我使用的是
cloud_firestore: ^1.0.3
【讨论】: