dart: get SQL like "%text%" search from firebase database snapshot
You are looking for a contains
query, but Firebase doesn't provide that. You can refine your actual query by looking for values that are equalTo
or endAt
or startAt
(these methods are available to you as well). Making three requests is going to be costly, though.
If your database is not large and your data is not sensitive, I suggest fetching the whole data and making a client-side filter as shown here.
Igor D
Updated on December 11, 2022Comments
-
Igor D over 1 year
i am currently working on a search function in my app. this gets a reference of my firebase database snapshot. im using the firebaseDatabase package for this.
currently im using this to search my database
FirebaseDatabase.instance.reference().child('products').orderByChild('title').equalTo(searchQuery)
the searchQuery variable is the the used string to search the database. this returns the list of products which the titles are equal to searchQuery.
searchQuery = 'hello'; title1 = 'hello'; title2 = 'hello there';
for now, only
searchQuery == title1; searchQuery != title2;
but i also want this to be returned
searchQuery == title2;
is this possible in the way im using this for now?
-
Igor D almost 5 yearsi saw that a query like .equalsTo(query + "\nf8ff" will return everything what comes after the query text. so query "hello"+"\nf8ff" will also return "hello there". so i thought "\nf8ff" + "there"+ "\nf8ff" would also return "hello there" but this is not the case. i dont know why such a basic query is not implemented :(
-
Hugo Passos almost 5 yearsWhere did you see that?
-
Igor D almost 5 yearsstackoverflow.com/questions/46460825/… i found multiple posts using this method. the problem is you can only query "abc%" and not "%abc%" :/