1

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();
  }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

3 Answers3

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?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 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 – Maldives Hunt Jun 13 '21 at 20:23
  • 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. – Frank van Puffelen Jun 13 '21 at 21:56
1

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,
        );
      },
    );
  }
tjheslin1
  • 1,378
  • 6
  • 19
  • 36
Keisuke
  • 11
  • 1
  • 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](/help/how-to-answer). – tjheslin1 Nov 28 '21 at 18:11
0

usually, it takes a few ms for data to retrieve so I tried this to make sure my operations are performed after data is retrieved

return StreamBuilder<QuerySnapshot>(
    stream: Collectionreference
        .snapshots(),
    builder: (BuildContext context,
        AsyncSnapshot<QuerySnapshot> activitySnapshot) {
      if (activitySnapshot.hasError) {
        return  Center(
              child: Text('Something went wrong'),
            );
      }
      if (activitySnapshot.connectionState == ConnectionState.waiting) {
        return Center(
                child: SpinKitWave(
              color: constants.AppMainColor,
              itemCount: 5,
              size: 40.0,
            )));
      }
      if (!activitySnapshot.hasData || activitySnapshot.data.docs.isEmpty) {
        return Center(
              child: Text('Nothing to Display here'),
            );
      }
      if (activitySnapshot.hasData) {
         activitySnapshot.data.docs.forEach(doc=>{
            print(doc);
          })
      }
     }
   });
Sanmit Vartak
  • 28
  • 1
  • 5
  • 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 Jun 13 '21 at 06:30