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

查看:97
本文介绍了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可能是异步获取的快照,这意味着在快照数据可用之前就构建了Widget,因此使其异步(如果我错了,请纠正我).这是我开始困惑的地方,快照到底是什么?什么是数据"?在每个人中.例如:为什么同一个函数无法在所有快照中检索所需的数据,而仅在特定快照上起作用.

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之间的确切区别是什么.当他们俩都返回地图时,为什么叫他们不同?

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中,您有 FutureBuilder StreamBuilder ,它们处理异步加载的数据的快照.

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 是Flutter对异步数据源数据的包装,例如Firestore和实时数据库.它们涵盖了这样的状态:从最初的连接到检索,直到出现错误或有数据为止,这些数据都可以存在.
  • DocumentSnapshot 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. 一个用于向您的 StreamBuilder 馈送的 AsyncSnapshot ,以便它可以呈现正确的数据加载状态.
  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.

实际上,我发现在代码中更容易看到它,例如在

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天全站免登陆