firebase - Flutter和Firebase:基本结构问题

标签 firebase flutter dart google-cloud-firestore

从逻辑上,结构上理解我应该如何使用Firebase时遇到一些问题。

我有一个目前看起来像这样的数据库:

enter image description here

在我使用的应用程序中:

StreamBuilder(
  stream: Firestore.instance.collection('stores').snapshots(),
  builder: (context, snapshot) {
    <code here>
   }
 ),

正如所有文档和指南所建议的。

但这会返回我所有文档(即我的所有用户ID)的循环。我不希望这样做,因为用户只能看到自己的数据。但是,如果我尝试将其更改为:
stream: Firestore.instance.collection('stores').document(uid).snapshots(),

然后是一个错误,因为您只能循环浏览文档集合,而不能循环浏览文档集合。

因此,由于此设计根本无法工作,我想也许在数据库中添加了另一个多余的级别,使其类似于:
Collection:
  - application name
  Document: 
    - Stores
      Collection:
         - UID of user
           Fields:
             - Datafield1
             - Datafield2
             - Datafield3
             - Datafield4
             - etc etc etc

试图迫使它符合可用的东西。但它也不允许您在Firebase中执行此操作,因为它仅会在文档上显示字段,而不会在集合上显示字段。

因此,我的问题是,如何使用该产品仅循环浏览一组用户数据?我敢肯定,这是对这一行的简单更改:
stream: Firestore.instance.collection('stores').snapshots(),

但是我在网上找不到任何关于此的信息。

编辑:@SenpaiLeo

以前我是这样返回数据的:
return ListView.builder(
  itemExtent: 500,
  itemCount: snapshot.data.documents.length,
  itemBuilder: (context, index) =>
      _buildListRows(context, snapshot.data.documents[index]),
);

然后在_buildListRows中:
Widget _buildListRows(BuildContext context, DocumentSnapshot document) {
...
  Container(
      child: TextField(
        controller: _storeNameController,
        decoration: InputDecoration(
           border: OutlineInputBorder(),
              hintText: document['store_name'],
           ),
        ),
    ),
    ...

所以现在可能是我根据您的建议更改了引用,现在是这样:
document['store_name']

引用不正确吗?

最佳答案

如果使用此:

Collection:
  - Stores
     Document:
        - UID of user
         Collection:
            - Datafield1
            - Datafield2
            - Datafield3
            - Datafield4
            - etc etc etc

然后执行:
Firestore.instance.collection('stores').document(uid).snapshots()

然后上面应该只获取uid文档中的数据,而不会获取子集合。如果要访问一个用户的子集合内的数据,请使用以下命令:
Firestore.instance.collection('stores').document(uid).collection('userStore').snapshots()

这将在子集合userStore中获取所有文档

解:
child: StreamBuilder(
    stream: Firestore.instance.collection('stores').document(currentUserUID).snapshots(),
    builder: (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
    if (snapshot.hasData) {
       return ListView.builder(
           shrinkWrap: true,
           itemCount: snapshot.data.data.length,
           itemBuilder: (context, index) =>
               _buildListRows(context, snapshot.data),
           );


Widget _buildListRows(BuildContext context, DocumentSnapshot document) {
return Container(
  height: MediaQuery.of(context).size.height * 0.8,
  child: ListView(
    children: <Widget>[
      Row(
        children: <Widget>[
          Padding(
            padding: EdgeInsets.fromLTRB(10, 0, 50, 0),
            child: Text(
              'Business Name',
              style: Theme
                  .of(context)
                  .textTheme
                  .body2,
            ),
          ),
          Container(
            height: 50,
            width: 200,
            padding: EdgeInsets.fromLTRB(5, 0, 5, 0),
            child: TextField(
              controller: _storeNameController,
              decoration: InputDecoration(
                border: OutlineInputBorder(),
                hintText: document['store_name'],
              ),
            ),
          ),
        ],
      ),

Firestore.instance.collection('stores').document(currentUserUID).snapshots()将获取当前用户的文档,然后通过snapshot.data.data["location"]对其进行访问

关于firebase - Flutter和Firebase:基本结构问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61696006/

相关文章:

flutter - 带有 IndexedStack 的 AnimatedSwitcher

android - 如何同时播放两种声音?

list - 在flutter中没有从json中获取用户名的所有记录

dart - 关于导入本地文件的Dart风格建议

dart - 如何动态实例化一个类(Function.apply for class)?

ios - 就推送通知而言,Firebase + Batch 是 Parse 的良好替代品吗?

javascript - 使用 async/await 获取 firebase.auth().currentUser

javascript - 单击按钮后使用 Firebase 重置/清除表单

flutter - 如何解决错误 : The sandbox is not in sync with the Podfile. 锁

android - Firebase Crashlytics SDK 无法识别 Crashlytics 类