【问题标题】:How can i get data from a subcollection in firebase in flutter?如何从颤动的firebase中的子集合中获取数据?
【发布时间】:2021-03-21 05:31:31
【问题描述】:

我正在使用 Flutter 和 Firebase 作为后端构建应用程序。 我将我的数据存储在名为“用户”的集合中。每个用户都有数据并有一个名为“交易”的子集合,这是他所做的交易。 我想得到这些信息 这是我的数据库

enter image description here

enter image description here

class DatabaseService {
      final String uid;
      DatabaseService({this.uid});
      final CollectionReference usersCollection =
         FirebaseFirestore.instance.collection('Users');

         List<Transaction1> TransactionsListFromSnapshot(QuerySnapshot snapshot) {
          return snapshot.docs.map((doc) {
          return Transaction1(
              uid: doc.data()['uid'],
              name: doc.data()['name'],
              description: doc.data()['description'],
              time: doc.data()['time'],
              argent: doc.data()['argent'],
              somme: doc.data()['somme'],
              deleted: doc.data()['deleted']);
              }).toList();
               }

      Stream<List<Transaction1>> get transactions{
        return  
    
usersCollection.doc(uid).collection('Transactions').snapshots().map(TransactionsListFromSnapshot);
       }
         }

这是颤振代码

       body: StreamBuilder<List<Transaction1>>(
      stream: DatabaseService(uid: this.widget.uid).transactions,
      builder: (context, snapshot) {
        return snapshot.hasData ? Stack(
          children: [
            ListView.builder(
              itemCount: snapshot.data.length,
              itemBuilder: (context, index) {
                print('hhhhh');
                return Transaction_Card(data: snapshot.data[index]);
              },
            ),
            Positioned(
                right: 20,
                bottom: 20,
                child: FloatingActionButton(
                  child: Icon(
                    Icons.add,
                    color: Colors.white,
                  ),
                  onPressed: () {
                    _showMyDialog();
                  },
                ))
          ],
        ) :  Stack(
          children: [
            Text('Error'),
            Positioned(
                right: 20,
                bottom: 20,
                child: FloatingActionButton(
                  child: Icon(
                    Icons.add,
                    color: Colors.white,
                  ),
                  onPressed: () {
                    _showMyDialog();
                  },
                ))
          ],
        );
      }),

谁能帮帮我

【问题讨论】:

    标签: firebase flutter dart


    【解决方案1】:

    我昨天刚做了这个。在我的父文档中,我添加了一个数组,其中存储了子集合文档的文档 ID。

    然后,在存储库中我有以下功能(项目是主文档,区域是子集合文档

      @override
      Stream<List<Area>> areas(String projectId) {
        final CollectionReference areaCollection =
        FirebaseFirestore.instance.collection('projects').doc(projectId).collection('areas');
    
        return areaCollection.snapshots().map((snapshot) {
          return snapshot.docs
              .map((doc) => Area.fromEntity(AreaEntity.fromSnapshot(doc)))
              .toList();
        });
      }
    

    然后,我使用example of the bloc documentation 与实体和模型来转换数据。

    对于每个(子)集合,我都有一个单独的监听器,一个用于项目,一个用于我当前关注的项目的领域。如果有新区域出现,我会检查我是否已经知道它们(通过 id),或者我是否必须在项目中添加一个新区域。

    for (var a in event.areas) {
          if (!areaPointer.containsKey(a.areaId)) { // add new area to list
            areaPointer[a.areaId] = project.areas.length;
            project.areas.add(a);
          }
          else                                      // update existing area
            project.areas[areaPointer[a.areaId]] = a;
        }
    

    如你所见,我还有一个areaPointer,它存储了area.id和project.areas[i]索引的关系

    【讨论】:

      猜你喜欢
      • 2021-07-23
      • 1970-01-01
      • 2021-07-25
      • 1970-01-01
      • 1970-01-01
      • 2022-10-06
      • 2021-06-01
      • 1970-01-01
      • 2018-03-24
      相关资源
      最近更新 更多