Home > other >  The getter 'documents' isn't defined for the type 'Object' - Flutter Error
The getter 'documents' isn't defined for the type 'Object' - Flutter Error

Time:02-27

I am trying to read image URL from firebase fire store. But I am getting this error which says

"The getter 'documents' isn't defined for the type 'Object'. Try importing the library that defines 'documents', correcting the name to the name of an existing getter, or defining a getter or field named 'documents' "

here is my code

import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';

class FirebaseData extends StatelessWidget {
  const FirebaseData({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: StreamBuilder(
        stream: FirebaseFirestore.instance.collection('cards').snapshots(),
        builder: (context, snapshot) {
          
          return ListView.builder(
            itemCount: snapshot.data!.documents.length,
            itemBuilder: (context, index) {
              
              DocumentSnapshot card = snapshot.data!.documents[index];

              return Image.network(card['image']);
            },
          );
        },
      ),
    );
  }
}

CodePudding user response:

Replace StreamBuilder() with StreamBuilder<QuerySnapshot>(). After that, you will be able to access documents.

Let me know if you face any issues.

  • Related