I have successfully filter the search results based on one field from the firestore database, what i'm trying to do now is to filter based on several fields from the firebase instead of one.
The code i presented is the entire Streambuilder with one value filter function, i hope someone can teach me how to filter multiple value based on my code, thanks in advance
TextField(
controller: searchController,
onChanged: (value) {
setState(() {
searchText = value;
});
},
decoration: InputDecoration(
hintText: 'Search...',
prefixIcon: Icon(Icons.search),
),
),
StreamBuilder(
stream: allNoteCollection.snapshots(),
builder: (ctx, streamSnapshot) {
if (streamSnapshot.connectionState ==
ConnectionState.waiting) {
return Center(
child: CircularProgressIndicator());
}
documents = streamSnapshot.data!.docs;
if (searchText.length > 0) {
documents = documents.where((element)
{
return element
.get('serviceName')
.toString()
.toLowerCase()
.contains(searchText.toLowerCase());
}).toList();
}
return ListView.separated(
reverse: true,
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
itemCount: documents.length,
separatorBuilder: (BuildContext context, int index) {
return Divider();
},
itemBuilder: (BuildContext context, int index) {
return ListTile(
contentPadding:
EdgeInsets.symmetric(horizontal: 0.0),
onTap: () {
},
title: Text(documents[index]['serviceName']),
subtitle: Text(documents[index]['serviceDetails']),
);
},
);
},
),
},
);
},
),
CodePudding user response:
Filtering on multiple values is no different than filtering on a single value, you just add more than on where
call to the query. See for an example of this, the Firebase documentation on compound queries.
The main difference is that you often need to explicitly add the composite index that is required for such a query. While single-field indexes are automatically added by Firestore, indexes consisting of multiple fields needs to be added explicitly in the Firestore console. If you run a query that requires an index that doesn't exist, it raises an error with a link to the Firestore console directly to the page where you can create the index - with all values already filled in exactly as needed. If you'd rather fill out all the details yourself, you can also go the page directly with this link.