Cannot get documents or docs with cloud firestore : ^2.1.0 flutter

Issue

I’ve just upgraded to cloud firestore: ^2.1.0 and I have an error on snapshots.data?.documents or snapshots.data?.docs with message The getter ‘documents’ isn’t defined for the type ‘Object’. in the above code :

@override
  Widget build(BuildContext context) {
    return StreamBuilder(
      stream: FirebaseFirestore.instance.collection('Courses').snapshots(),
      builder: (context1, snapshot) {
        //just add this line
        if (snapshot.data == null) return CircularProgressIndicator();
        return ListView.builder(
          itemCount: snapshot.data?.documents.length,
          itemBuilder: (context2, index) {
            return GestureDetector(
              onTap: () => Navigator.of(context).push(
                MaterialPageRoute(
                  builder: (context) => DetailCourseScreen(
                      snapshot.data?.documents[index]['title'],
                      snapshot.data?.documents[index]['storage'],
                      index),
                ),
              ),
              child: _buildCourseList(
                  context2, snapshot.data?.documents[index], index),
            );
          },
        );
      },
    );
  }

And here is my pubspec.yaml :

environment:
  sdk: '>=2.12.0 <3.0.0'

dependencies:

  cloud_firestore: ^2.1.0
  firebase_core: ^1.1.1
  firebase_storage: ^8.0.6
  firebase_image: ^1.0.1

Solution

I understand your confusion. Accessing cloud firestore’s data has gotten kind of weird since the update on cloud_firestore 2.0.0. You can access your data with the following code:

StreamBuilder<QuerySnapshot<Map<String, dynamic>>>(
      stream: FirebaseFirestore.instance
          .collection('Courses')
          .withConverter<Map<String, dynamic>>(
            fromFirestore: (snapshot, _) => snapshot.data() ?? Map<String, dynamic>(),
            toFirestore: (model, _) => Map<String, dynamic>.from(model as Map),
          )
          .snapshots(),
      builder: (context, snapshot) {
        if (snapshot.data == null) return CircularProgressIndicator();
        return ListView.builder(
          itemCount: snapshot.data?.docs.length,
          itemBuilder: (context2, index) {
            return GestureDetector(
              onTap: () => Navigator.of(context).push(
                MaterialPageRoute(
                  builder: (context) => DetailCourseScreen(
                      snapshot.data?.docs[index].data()['title'],
                      snapshot.data?.docs[index].data()['storage'],
                      index),
                ),
              ),
              child: _buildCourseList(context2, snapshot.data?.docs[index].data(), index),
            );
          },
        );
      },
    );

You can read more about this update here: https://pub.dev/packages/cloud_firestore/changelog#200

Answered By – dshukertjr

Answer Checked By – Robin (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *