【问题标题】:Displaying a specific Firestore document in Flutter在 Flutter 中显示特定的 Firestore 文档
【发布时间】:2021-07-12 08:06:49
【问题描述】:

我只想在 Flutter 中显示 Firestore 中的特定文档。为此,我尝试使用if 语句,但我只收到一个没有数据的空白屏幕。如果我删除if 语句,那么我将显示所有文档。

【问题讨论】:

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


    【解决方案1】:

    除了获取整个集合之外,您还可以通过此获取特定文档。在这种情况下,无需添加 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

    【讨论】:

      猜你喜欢
      • 2020-10-05
      • 2019-04-11
      • 2021-10-22
      • 2021-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-03
      • 1970-01-01
      相关资源
      最近更新 更多