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,
);
},
);
}
Author by
Maldives Hunt
Updated on December 30, 2022Comments
-
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 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 almost 3 yearsso what do i have to do to fix my code?
-
pskink almost 3 yearsuse
AsyncSnapshot.data
property -
Maldives Hunt almost 3 years@pskink can you please tell how i can use
AsyncSnapshot.data
property here -
pskink almost 3 yearswhat do you see if you call
print(snapshot.data)
? if it is notnull
then it has your data -
Maldives Hunt almost 3 yearsit is null what change do I have to get data with my code?
-
pskink almost 3 yearsadd
print(snapshot)
then - it seems thaterror
is set -
Maldives Hunt almost 3 yearsAsyncSnapshot<QuerySnapshot<Object?>>(ConnectionState.done, Instance of '_JsonQuerySnapshot', null, null)
-
pskink almost 3 yearsso it is not
null
- it is_JsonQuerySnapshot
which is a concrete implementation of QuerySnapshot class -
Maldives Hunt almost 3 yearsso how do i display my data?
-
-
Maldives Hunt almost 3 yearstried 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 almost 3 yearsThank 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 almost 3 yearsI 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 over 2 yearsYour 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.