Harsh Patel
Harsh Patel

Reputation: 269

How to filter out matching usernames form firestore database in flutter social media application

So I am creating a Social media appliation with the help of flutter. And in search user page I had created a page where user can search their friends with their Profile names. And in this I used:-

Future allUsersProfile = FirebaseFirestore.instance.collection("users").where("profileName", isGreaterThanOrEqualTo: str).get();

By this I get all the matching profile names.

But when I enter their last name their is no result.

Also I want to convert profile names to lower case and then match.

Also I want that if the search name is in between the saved profile name(for example :- I want to search "arsh" form "Harsh", I can't find the result. ) then also it should give this particular result,

So I want idea that how big social media companies search users.

Can anyone can help me.

Thanks in advance

Upvotes: 0

Views: 578

Answers (1)

Huthaifa Muayyad
Huthaifa Muayyad

Reputation: 12373

Assuming you are storing profileName in the same field(including first and last names). i.e, in your Firebase user document, profileName combines both first and last names. You need to do the following:

1- In your Firebase users collection, store a new field for every document called something like 'searchName'.

2- When you store your user info, use this code to to convert profileName into searchName:

String profileName = 'Huthaifa Muayyad`;
String searchName = profileName.toLowerCase().replaceAll(' ', ''); 
//The .toLowerCase method converts it all to lower case
//The replaceAll, removes the whitespaces. 
//Notice the difference between  (' ', ''). 
//This will result in Huthaifa Muayyad => huthaifamuayyad

3- Save both fields in firebase along with all the other info when you create the user.

4- Assuming that str is the input you are getting from the search field, modify your Firebase query to look like this:

Future allUsersProfile = FirebaseFirestore.instance.collection("users").where("searchName", isGreaterThanOrEqualTo: str.toLowerCase().replaceAll(' ', '')).get();

This should solve your problem.

Upvotes: 1

Related Questions