简体   繁体   English

Flutter/Firebase - 列表<dynamic>没有实例 getter 'documents'</dynamic>

[英]Flutter/Firebase - List<dynamic> has no instance getter 'documents'

I am trying to combine two firestore streams in my Flutter application.我正在尝试在我的 Flutter 应用程序中组合两个 firestore 流。 However when I try and implement the merged stream I get the following error:但是,当我尝试实施合并的 stream 时,出现以下错误:

    The following NoSuchMethodError was thrown building StreamBuilder<dynamic>(dirty, state: _StreamBuilderBaseState<dynamic, AsyncSnapshot<dynamic>>#25b40):
Class 'List<dynamic>' has no instance getter 'documents'.
Receiver: _List len:2
Tried calling: documents

I assume there is another step I need to implement when I am establishing my stream before I can use it in a Pageview builder?我假设在建立 stream 之前我需要执行另一个步骤,然后才能在 Pageview 构建器中使用它? Here are my streams:这是我的流:

Stream<QuerySnapshot> getDefaultOccasions(BuildContext context) async*{
  yield* Firestore.instance.collection('datestoremember').document('default').collection('Dates_to_Remember').snapshots();
}

Stream<QuerySnapshot> getPersonalOccasions(BuildContext context) async*{
  final uid = await Provider.of(context).auth.getCurrentUID();
  yield* Firestore.instance.collection('datestoremember').document(uid).collection('Dates_to_Remember').snapshots();
}

 getData() {
  Stream stream1 = Firestore.instance.collection('datestoremember').document('default').collection('Dates_to_Remember').snapshots();
  Stream stream2 = Firestore.instance.collection('datestoremember').document('Ngrx54m84JbsL0tGvrBeKCBlEnm2').collection('Dates_to_Remember').snapshots();
  return StreamZip([stream1, stream2]);
}

I then go and implement this here:然后我 go 并在这里实现:

              child: StreamBuilder(
            stream: getData(),
            builder: (context, snapshot) {
              if(!snapshot.hasData) return const Text("Loading...");
              return new PageView.builder(
                itemCount: snapshot.data.documents.length,
                controller: PageController(viewportFraction: 0.5),
                onPageChanged: (int index) => setState(() => _index = index),
                itemBuilder: (_, i) {
                  return Transform.scale(
                    scale: i == _index ? 1 : 0.5,
                    child: Card(
                      elevation: 6,
                      shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(10)),
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Text(snapshot.data.documents[i]['Date'].toDate().day.toString()),
                          Text(DateFormat.MMMM()
                              .format(
                              formatter.parse(snapshot.data.documents[i]['Date'].toDate().toString()))
                              .toString()),
                          Padding(
                            padding: const EdgeInsets.only(
                                left: 8.0, right: 8.0),
                            child: FittedBox(
                              fit: BoxFit.contain,
                              child: Text(
                                snapshot.data.documents[i]['Title'],
                                overflow: TextOverflow.ellipsis,
                              ),
                            ),
                          )
                        ],
                      ),
                    ),
                  );
                },
              );},
          ),

Any ideas?有任何想法吗? Cheers干杯

Try this:试试这个:

getData() {
  Stream stream1 = Firestore.instance.collection('datestoremember').document('default').collection('Dates_to_Remember').snapshots();
  Stream stream2 = Firestore.instance.collection('datestoremember').document('Ngrx54m84JbsL0tGvrBeKCBlEnm2').collection('Dates_to_Remember').snapshots();

  return StreamGroup.merge([stream1, stream2]).asBroadcastStream();
}

EDIT using StreamZip:使用 StreamZip 进行编辑:

Stream<List<QuerySnapshot>> getData() {
  Stream stream1 = Firestore.instance.collection('datestoremember').document('default').collection('Dates_to_Remember').snapshots();
  Stream stream2 = Firestore.instance.collection('datestoremember').document('Ngrx54m84JbsL0tGvrBeKCBlEnm2').collection('Dates_to_Remember').snapshots();
  return StreamZip<List<QuerySnapshot>>([stream1, stream2]);
}

And in StreamBuilder:在 StreamBuilder 中:

child: StreamBuilder(
        stream: getData(),
        builder: (BuildContext context, AsyncSnapshot<List<QuerySnapshot>> snapshot) {

          List<QuerySnapshot> combinedSnapshot = snapshot.data.toList();
          combinedSnapshot[0].documents.addAll(combinedSnapshot[1].documents);

          if(!combinedSnapshot[0].hasData) return const Text("Loading...");
          return new PageView.builder(
            itemCount: combinedSnapshot[0].data.documents.length,
            controller: PageController(viewportFraction: 0.5),
            onPageChanged: (int index) => setState(() => _index = index),
            itemBuilder: (_, i) {
              return Transform.scale(
                scale: i == _index ? 1 : 0.5,
                child: Card(
                  elevation: 6,
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(10)),
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Text(combinedSnapshot[0].data.documents[i]['Date'].toDate().day.toString()),
                      Text(DateFormat.MMMM()
                          .format(
                          formatter.parse(combinedSnapshot[0].data.documents[i]['Date'].toDate().toString()))
                          .toString()),
                      Padding(
                        padding: const EdgeInsets.only(
                            left: 8.0, right: 8.0),
                        child: FittedBox(
                          fit: BoxFit.contain,
                          child: Text(
                            combinedSnapshot[0].data.documents[i]['Title'],
                            overflow: TextOverflow.ellipsis,
                          ),
                        ),
                      )
                    ],
                  ),
                ),
              );
            },
          );},
      ),

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter firestore QuerySnapshot 在 Android 中没有 getter“文档”的实例 - Flutter firestore QuerySnapshot has no instance of getter 'documents' in Android 列出子集合中所有文档的名称 - Firebase Flutter - List names of all documents in subcollection - Firebase Flutter Class 'List<_JsonQueryDocumentSnapshot>' 没有实例 getter 'lenght'。 接收器:“_GrowableList”的实例(长度:3) 尝试调用:lenght - Class 'List<_JsonQueryDocumentSnapshot>' has no instance getter 'lenght'. Receiver: Instance(length:3) of '_GrowableList' Tried calling: lenght Flutter Firebase:检索文档列表,仅限于数组中的 ID? - Flutter Firebase: Retrieve a list of documents, limited to IDs in an array? Flutter Firebase Cloud Firestore 通过 ID 获取文档列表 stream - Flutter Firebase Cloud Firestore get stream of list of documents by their ids 无法从 Firebase 子集合 Flutter 中获取文档列表 - Not able to get a list of documents from Firebase Subcollection Flutter 错误:Class '_JsonDocumentSnapshot' 没有实例 getter 'docs' - Error: Class '_JsonDocumentSnapshot' has no instance getter 'docs' 尝试使用 flutter 访问 Firebase 上的列表时,在 Null 错误上调用获取 getter 长度 - Getting a getter length is called on Null error when trying to access List on Firebase using flutter 将 firebase 个文档添加到列表中 - Adding firebase documents to a list Class '列表<documentsnapshot> ' 在 Flutter 中没有实例方法 'call'</documentsnapshot> - Class 'List<DocumentSnapshot>' has no instance method 'call' in Flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM