【问题标题】:Can I listen to a single document in Firestore with a StreamBuilder?我可以使用 StreamBuilder 在 Firestore 中收听单个文档吗?
【发布时间】:2019-10-23 20:10:35
【问题描述】:

我正在尝试监听集合的单个文档中的更改,但我无法让它工作。

Widget build(BuildContext context) {
  return StreamBuilder(
    stream: Firestore.instance.collection("events").document(widget.documentID).get().asStream(),
    ...
  }
}

我正在使用 .asStream() 方法,但我只获取一次文档。如果我在 Firebase 控制台中更改数据,除非我重新打开视图,否则不会有任何更新。

有没有办法做到这一点?

【问题讨论】:

    标签: firebase flutter dart google-cloud-firestore


    【解决方案1】:

    您只获取一次文档的原因是因为 DocumentReference.get 方法最初只返回一个 Future 而使用 asStream 只会返回那个一个单个FutureStream

    不过,cloud_firestore 包有一个内置的正确监听文档的方法。
    您可以改用DocumentReference.snapshots,它会返回该文档每次更改的Stream

    在您的代码中,您只需将.get().asStream() 替换为.snapshots()

    【讨论】:

    • 很好的答案。我什至不知道DocumentReference.snapshots() 的存在,它非常适合这个用例。在其他平台上,您必须在 FieldPath.documentId 上进行查询,如 Denys 的回答所示:stackoverflow.com/a/52252264
    【解决方案2】:

    如果您想获取单个文档,请使用 Future builder 而不是 Stream Builder。

    FutureBuilder<DocumentSnapshot>(
                                                  future: Firestore.instance
                                                      .collection('users')
                                                      .document(widget.puid)
                                                      .get(),
                                                  builder: (context,
                                                      AsyncSnapshot<
                                                              DocumentSnapshot>
                                                          snapshot) {
                                                    if (snapshot.hasError)
                                                      return Center(
                                                        child: Text(snapshot
                                                            .hasError
                                                            .toString()),
                                                      );
                                                    return snapshot.hasData
                                                        ? Text(
                                                            "${snapshot.data['username']}",
                                                            style: TextStyle(
                                                                color:
                                                                    kPrimaryColor,
                                                                fontSize: 18,
                                                                fontWeight:
                                                                    FontWeight
                                                                        .bold),
                                                          )
                                                        : Container();
                                                  },
                                                ),
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-21
      • 1970-01-01
      • 1970-01-01
      • 2023-01-01
      • 1970-01-01
      • 2018-06-01
      相关资源
      最近更新 更多