简体   繁体   中英

when fetching firestore database "itemCount: snapshot.data.documents.length" is not working anymore. does anyone know how to solve this problem?

this is the code:...............

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("Practice Firebase"),
        ),
        body: StreamBuilder(
            stream:
                FirebaseFirestore.instance.collection("Animals").snapshots(),
            builder: (context, snapshot) {
              if (!snapshot.hasData) {
                return Center(
                  child: const CircularProgressIndicator.adaptive(),
                );
              } else {
                return ListView.builder(
                    itemCount: snapshot.data?.documents.length, 
                    itemBuilder: (context, index) {
                      return ListTile(
                        title: snapshot.data.documents[index]["name"],
                      );
                    });
              }
            }));
  }
}

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'

I had the same problem and this fixed it for me. So basically you need to give the snapshot the datatype of AsyncSnapshot since .docs cannot be called on an Object type.

StreamBuilder(
builder: (context, AsyncSnapshot snapshot) {
        return ListView.builder(
          itemCount: snapshot.data?.docs.length,
         

You can try to use the code skeleton below (here I have an own class MyAnimal so I can use members instead of map):

  StreamBuilder<QuerySnapshot>(
    stream: FirebaseFirestore.instance
        .collection('Animals')
        .snapshots(),
    builder: (BuildContext context,
        AsyncSnapshot<QuerySnapshot> snapshot) {
      if (snapshot.hasError) {
        // handle error
        return const Text('Error');
      }

      if (snapshot.connectionState == ConnectionState.waiting) {
        // return progress indicator widget
        return const Center(child: CircularProgressIndicator());
      }

      // here you should get the streamed documents like:
      // snapshot.data!.docs

      // so to build a ListView for example:

      return ListView(
        children:
            snapshot.data!.docs.map((DocumentSnapshot document) {
          final animal = document.data() as MyAnimal;
          return ListTile(
            title: Text(animal.name!),
          );
        }).toList(),
      );
    })

just declare the dataType of builder arguments..i mean, builder:(BuildContext context,AsyncSnapshot snapShot) enter image description here

Well actually, if you do this - you can access docs and length...

builder: (BuildContext ctx, AsyncSnapshot LessThanSign QuerySnapshot GreaterThanSign streamSnapShot){}

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