Firebase 中现有类型的快照之间有什么区别? [英] What is the difference between existing types of snapshots in Firebase?

查看:20
本文介绍了Firebase 中现有类型的快照之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

随着我的代码和我在 Flutter 中的编码经验的进一步发展.在使用 Firebase API 时,我遇到了不同类型的快照.我说的是 AsyncSnapshots、QuerySnapshots、DocumentSnapshots 和 DataSnapshots.如果有更多请也给他们起名字.

as I'm further progressing with my code and my coding experiences in Flutter. I've encountered different types of snapshots, while working with the Firebase API. I'm talking about AsyncSnapshots, QuerySnapshots, DocumentSnapshots and DataSnapshots. If there are more pls name them too.

我想知道这些快照之间的确切区别是什么.

I was wondering, what the exact differences are between those snapshots.

到目前为止,我认为 AsyncSnapshot 可能是异步拍摄的快照,这意味着小部件是在快照数据可用之前构建的,因此使其异步(如果我错了,请纠正我).这是我的困惑开始的地方,快照到底是什么?什么是数据"?在他们每个人中.例如:为什么同一个函数不能检索所有快照中所需的数据,而只能在特定快照上运行.

What I've figured so far is that an AsyncSnapshot is probably a Snapshot that is taken asynchronously, meaning the Widget is built before the Data of the Snapshot is available therefore making it async (pls correct me if I'm wrong). Here is where my confussion starts, what exactly is a Snapshot? And what is the "Data" in each of them. For example: Why can't the same function retrieve the needed Data in all of the Snapshots, but is only functioning on a particular Snapshot.

为什么需要将数据从 QuerySnapshot 转换为 DocumentSnapshot,以使其可访问(如果我错了,请再次纠正我)?DocumentSnapshot 和 DataSnapshot 之间的确切区别是什么.当它们都返回 Maps 时,为什么它们的调用方式不同?

Why is it necessary to convert the Data from a QuerySnapshot to a DocumentSnapshot, to make it accesible (Again pls correct me if I'm wrong)? And what is the exact difference between DocumentSnapshot and DataSnapshot. And why are they called differently, when both of them return Maps?.

提前致谢.

推荐答案

据我所知,你是在 Flutter 上下文中问这个问题的,所以我会在下面回答这个问题.

As far as I can tell you are asking this in the context of Flutter, so I'll answer for that below.

Firebase 中有两个数据库:原始实时数据库和较新的 Cloud Firestore.今天两者都是同样有效的选项,但它们与自己的 API 完全分开.但两者都返回数据的快照,其中快照是应用程序代码中数据库中数据的副本.

There are two databases in Firebase: the original Realtime Database, and the newer Cloud Firestore. Both are equally valid options today, but they are completely separate with their own API. But both return snapshots of data, where the snapshot is a copy of the data from the database in your application code.

在 Flutter 中有 FutureBuilderStreamBuilder,它们处理异步加载的数据快照.

In Flutter you have FutureBuilder and StreamBuilder, which deal with snapshots of data that is loaded asynchronously.

让我们看看我是否可以覆盖它们:

Let's see if I can cover them:

  • AsyncSnapshot 是 Fl​​utter 对来自异步数据源的数据的封装,例如 Firestore 和实时数据库.它们涵盖了此类数据可能处于的状态,从初始连接到检索,直到出现错误或拥有数据.
  • DocumentSnapshots 和 QuerySnapshots 是 Firestore 的类,用于表示单个文档或您获得的文档列表从数据库读取时.因此,如果您加载单个文档,您将获得一个带有其数据的 DocumentSnapshot.如果你加载一个文档列表,你会得到一个 QuerySnapshot,然后你可以循环访问各个 DocumentSnapshot.
  • A DataSnapshot 是实时数据库的类,用于单个节点,以及来自数据库的节点列表.
  • An AsyncSnapshot is Flutter's wrappers around data from asynchronous data sources, such as Firestore and Realtime Database. They cover the states that such data can be in, from the initial connection, through retrieval, until errors or having the data.
  • DocumentSnapshots and QuerySnapshots are Firestore's classes to either represent a single document, or a list of documents that you get when reading from the database. So if you load a single document, you get a DocumentSnapshot with its data. And if you load a list of document, you get a QuerySnapshot that you then loop over to access the individual DocumentSnapshots.
  • A DataSnapshot is the Realtime Database's class for both a single Node, and a list of nodes from the database.

因此,在 Flutter 中,您将有一个 AsyncSnapshot 引用 Firebase 快照类之一,然后该 Firebase 快照包装实际数据.

So in Flutter you'll have an AsyncSnapshot that refers to one of the Firebase snapshot classes, and that Firebase snapshot then wraps the actual data.

假设您想显示一个包含 Firestore 集合中文档的列表,您将:

Say you want to display a list with the documents in a collection from Firestore, you'll have:

  1. 一个 AsyncSnapshot 提供给您的 StreamBuilder,以便它可以呈现数据加载的正确状态.
  2. 用于数据库中文档列表的 QuerySnapshot.
  3. 该列表中的每一项都是一个DocumentSnapshot,其中包含来自单个文档的数据快照.
  1. An AsyncSnapshot to feed to your StreamBuilder, so that it can render the correct state of data loading.
  2. A QuerySnapshot for the list of documents from the database.
  3. Each item in that list is then a DocumentSnapshot with a snapshot of the data from a single document.

实际上,我发现这在代码中更容易看到,就像 FlutterFire 文档:

I actually find this much easier to see in code, as in this example from the FlutterFire documentation:

class UserInformation extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    CollectionReference users = FirebaseFirestore.instance.collection('users');

    return StreamBuilder<QuerySnapshot>(
      stream: users.snapshots(),
      builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
        if (snapshot.hasError) {
          return Text('Something went wrong');
        }

        if (snapshot.connectionState == ConnectionState.waiting) {
          return Text("Loading");
        }

        return new ListView(
          children: snapshot.data.documents.map((DocumentSnapshot document) {
            return new ListTile(
              title: new Text(document.data()['full_name']),
              subtitle: new Text(document.data()['company']),
            );
          }).toList(),
        );
      },
    );
  }
}

这篇关于Firebase 中现有类型的快照之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆