Firebase snapshot.data.documents 颤抖:我到底在做什么?

Firebase snapshot.data.documents with flutter: what exactly am I doing here?

我想更好地了解 firebase 的工作原理?

假设我有一个 firestore 数据库,并且有一个 collection ("books"),并且我有几个包含不同信息的文档,例如书名、作者、url 的图书图像,发布日期等。我想在列表中显示信息。这是我的代码

class BookList extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return StreamBuilder<QuerySnapshot>(
      stream: _firestore
          .collection('books')
          .snapshots(),
      builder: (context, snapshot) {
            final booksDB = snapshot.data.documents;
            List<Book> books = [];
            for (var rest in booksDB) {
              final restName = rest.data['name'];
              final restURL = rest.data['url'];

                final book = Book(
                  name: restName,
                  url: restURL,
                );
                books.add(book);
            }

            return ListView(
              children: books,
            );
        }
      },
    );
  }
}

class Book extends StatelessWidget {
  Book({this.name, this.url});

  final String name;
  final String url;

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: <Widget>[
        Container(
          margin: EdgeInsets.all(10.0),
            child: new CachedNetworkImage(
                imageUrl: url != null
                    ? url
                    : 'https://webhostingmedia.net/wp-content/uploads/2018/01/http-error-404-not-found.png',
                placeholder: (context, url) => new CircularProgressIndicator(),
                errorWidget: (context, url, error) => new Icon(Icons.error),
                width: MediaQuery.of(context).size.width - 20,
                height: (MediaQuery.of(context).size.width - 20) / 3 * 2,
                fit: BoxFit.cover),

        ),
        Positioned(
            bottom: 20.0,
            left: 20.0,
            child: Text(name,
                style: kSendButtonTextStyle.copyWith(
                  backgroundColor: kColorPrimary.withOpacity(0.5),
                ))),
      ],
    );
  }
}

这是我的问题:我在写 final booksDB = snapshot.data.documents; 的时候到底在做什么? 我是否下载了所有文件和所有相关字段?还是我只是在创建参考?我想知道我是下载所有信息还是只下载我添加到图书列表中的信息(名称和 url)

这对我很重要,因为我想控制我的 firebase 读取,如果我不使用它,我不需要读取它...

我希望问题很清楚。谢谢

a collection ("books") and I have several documents containing different info like title, authors, url of the book image, publication date, etc.

这会奏效,但我认为这不是预期的方式。你看,一个书集应该包含几个书的文档。一份文件包含一本书的所有信息。您所做的是将一本书数据传播到多个文档中,这样您将无法在同一集合中添加超过一本书。应该是这样的:

_firestore.collection('books').document(bookID); //This accesses one book.
//The name of the document is the book id which you can specify manually or automatically, but it should be unique.  

然后在每个文档中你可以有多个字段。

Here is my question: what am I exactly doing when I am writing final booksDB = snapshot.data.documents;? Am I downloading all the documents and all the related fields? Or am I just creating a reference? I would like to know if I am downloading all the info or just the one I am adding to the books list (name and url)

因为你有 snapshot 这意味着文档已经下载到 snapshot 对象中。 booksDB这里只是参考已经下载的文档

这里有一些有用的参考资料: