简体   繁体   中英

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

I am trying to combine two firestore streams in my Flutter application. However when I try and implement the merged stream I get the following error:

    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? 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:

              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:

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:

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,
                          ),
                        ),
                      )
                    ],
                  ),
                ),
              );
            },
          );},
      ),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM