The getter 'docs' isn't defined for the type 'AsyncSnapshot<Object?>'

1,832

Solution 1

The snapshot in your code is an AsyncSnapshot, which indeed doesn't have a docs child. To get the docs from Firestore, you need to use:

snapshot.data.docs

Also see the FlutterFire documentation on listening for realtime data, which contains an example showing this - and my answer here explaining all snapshot types: What is the difference between existing types of snapshots in Firebase?

Solution 2

change like this:

return FutureBuilder(
      future: searchResultsFuture,
      builder: (context, **AsyncSnapshot** snapshot) {
        if (!snapshot.hasData) {
          return circularProgress();
        }
        List<UserResult> searchResults = [];
        **snapshot.data!.docs.forEach((doc) {** 
          User user = User.fromDocument(doc);
          UserResult searchResult = UserResult(user);
          searchResults.add(searchResult);
        });
        return ListView(
          children: searchResults,
        );
      },
    );
  }
Share:
1,832
Maldives Hunt
Author by

Maldives Hunt

Updated on December 30, 2022

Comments

  • Maldives Hunt
    Maldives Hunt over 1 year

    After migrating to null safety getting error The getter 'docs' isn't defined for the type 'AsyncSnapshot<Object?>'. Try importing the library that defines 'docs', correcting the name to the name of an existing getter, or defining a getter or field named 'docs'.

    Code snippet where error is

        return FutureBuilder(
          future: searchResultsFuture,
          builder: (context, snapshot) {
            if (!snapshot.hasData) {
              return circularProgress();
            }
            List<UserResult> searchResults = [];
            snapshot.docs.forEach((doc) {    //have error here
              User user = User.fromDocument(doc);
              UserResult searchResult = UserResult(user);
              searchResults.add(searchResult);
            });
            return ListView(
              children: searchResults,
            );
          },
        );
      } 
    

    searchResultsFuture

    
      handleSearch(String query) {
        Future<QuerySnapshot> users =
            usersRef.where("displayName", isGreaterThanOrEqualTo: query).get();
        setState(() {
          searchResultsFuture = users;
        });
      }
    
      clearSearch() {
        searchController.clear();
      }
    
    • pskink
      pskink almost 3 years
      "The getter 'docs' isn't defined for the type 'AsyncSnapshot<Object?>" - yes indeed AsyncSnapschot does not have a field called docs
    • Maldives Hunt
      Maldives Hunt almost 3 years
      so what do i have to do to fix my code?
    • pskink
      pskink almost 3 years
      use AsyncSnapshot.data property
    • Maldives Hunt
      Maldives Hunt almost 3 years
      @pskink can you please tell how i can use AsyncSnapshot.data property here
    • pskink
      pskink almost 3 years
      what do you see if you call print(snapshot.data)? if it is not null then it has your data
    • Maldives Hunt
      Maldives Hunt almost 3 years
      it is null what change do I have to get data with my code?
    • pskink
      pskink almost 3 years
      add print(snapshot) then - it seems that error is set
    • Maldives Hunt
      Maldives Hunt almost 3 years
      AsyncSnapshot<QuerySnapshot<Object?>>(ConnectionState.done, Instance of '_JsonQuerySnapshot', null, null)
    • pskink
      pskink almost 3 years
      so it is not null - it is _JsonQuerySnapshot which is a concrete implementation of QuerySnapshot class
    • Maldives Hunt
      Maldives Hunt almost 3 years
      so how do i display my data?
  • Maldives Hunt
    Maldives Hunt almost 3 years
    tried this but it does not fix my issue, still getting the same error, i have started getting this error only after migrating to null safety
  • Maldives Hunt
    Maldives Hunt almost 3 years
    Thank you for providing those information's, but still i haven't being able to fix my code I'm very new to flutter and don't know much on using these code, I'm just trying to learn them
  • Frank van Puffelen
    Frank van Puffelen almost 3 years
    I gathered as much, which is why I provided two links where you can find a deeper explanation and more example code. If you're having trouble making those work, edit your question to show what you've tried with the information/code from the answer and links.
  • tjheslin1
    tjheslin1 over 2 years
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.