Reputation: 85
I want to create a search functionality for my project to fetch data from firestore. I am trying to create a Flutter search that searches documents from Firebase, but I am getting the error below:
The getter 'documents' isn't defined for the type 'QuerySnapshot'. 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'.
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:nethouese/pages/views/service2/searchService.dart';
class search extends StatefulWidget {
@override
_searchState createState() => _searchState();
}
class _searchState extends State<search> {
TextEditingController _searchController= TextEditingController();
var QueryResult=[];
var tempsearchstore=[];
@override
void initialSearch(String value) {
if(value.length==0){
setState(() {
QueryResult=[];
tempsearchstore=[];
});
}
var capitalizedValue=value.substring(0,1).toUpperCase()+value.substring(1);
if(QueryResult.length==0&& value.length==1){
SearchService().searchByName(value).then((QuerySnapshot docs){
//Below are the two lines of code where the error is occurring.
for(int i=0;i<QuerySnapshot.documents.length)
QueryResult.add(docs.documents[i].data);
});
}
}
Widget build(BuildContext context) {
return Container(
child: Column(
children: [
Text('serch bar'),
TextField(
onChanged: (value){
initialSearch(value);
},
controller: _searchController,
decoration: InputDecoration(
prefixIcon: IconButton(
color: Colors.black,
icon: Icon(Icons.arrow_back),
iconSize: 20.0,
onPressed:(){
Navigator.of(context).pop();
},
)
),
)
],
),
);
}
}
Below is a screenshot of the lines of code where the error is occuring:
i also get the same Error even if i chaged QuerySnapshot to DocumentSnapshot enter image description here
Upvotes: 4
Views: 4701
Reputation: 12383
Try by changing this:
for(int i=0;i<QuerySnapshot.documents.length)
QueryResult.add(docs.documents[i].data);
To this:
for(int i=0;i<docs.docs.length)
QueryResult.add(docs.docs[i].data());
Explanation, you are using (QuerySnapshot docs)
for the for loop, so what you want be checking, it the length of docs
contained withing QuerySnapshot docs
you referenced above.
In docs.docs[i].data()
, the first docs is the name you used, the second docs is the actual list of documents retrieved from your Firebase query. I would advise to use better naming conventions, to cause less confusion, for example (QuerySnapshot queryResults)
.
Later on we can use queryResults.docs.length
instead of docs.docs.length
and docs.docs[i].data()
For example, this is more readable, and easier to debug, and would also work if you replace it in your code:
SearchService().searchByName(value).then((QuerySnapshot queryResults){
for(int i=0;i<queryResults.docs.length) // Use queryResults.documents.length, #if you are using the older version of Firestore dependency.
QueryResult.add(queryResults.docs[i].data()); //queryResults.documents[i].data() #same reason as above
});
}
You also have to use data()
to unlock the content of this document at this index "docs[i]"
, otherwise you would get "instanceOfDocumentSnapshot
" instead of your actual results that you want to add to your List QueryResult
.
Upvotes: 2
Reputation: 737
You're trying to get a document, not a query. Therefore, you need to replace QuerySnapshot with DocumentSnapshot
Upvotes: 0