How can I perform filtering/searching school by writing the email address in flutter? - flutter

I would like to implement filtering/searching the university by typing its name like this picture below using flutter as a beginner. I would like it to auto-filter then display the school name even when I don't type the whole email address. It is quite hard to find good examples for what I want to do. Does anyone know of good examples that implemented something like I want?
Thank you

This a simple search in list items based on the text written. Below is the solution i use to search items in a list and update the list accordingly.
Assume you have a list of Emails.
List emails = []; A list which consist of emails from server/source
List emailsToShow = []; List of emails you update with every letter typed.
searchEmail(String typedLetters) {
if (typedLetters.length > 0) {
emailsToShow.clear();
emailsToShow.addAll(emails
.where((element) =>
element.toLowerCase().contains(typedLetters.toLowerCase()))
.toList());
setState((){});
}
}
Now call this function from onChanged method of TextField and show emailsToShow list in ListView/ListView.builder and you are good to go.

Related

Fastest way to search through nested lists

So we have a list of objects where every object(restaurant) contains a list of object(items). And I’m trying to implement a search items method where the user will be able to directly search through items .
I already thought about going through all the restaurants and add every restaurant’s items to a list of items and search through that list .
But this solution doesn’t seem scalable especially if we will have a lot of restaurants with a lot of items .
class Restaurant {
String name,
List<Item> items
…
}
class Item{
String name,
… }
For the performance and time optimization, you have to look for the solution from the prespective of Algorithms and Data Structure.
But if you are looking for a simnple soution for the problem you can just do a nested loop for the search like the following:
String? searchItem(String searchQuery){
for(Restaurant restaurant in restaurants){
for(Item item in restaurant.items)
{
if(item.name.contains(searchQuery))
{
return item.name;
}
}
}
return null; // No Item Found!
}
NOTE: This implementation will do early return once it find an item, so the Best Case would be if the item is first one in first restaurant Ω(1), and the Worst Case would be it the item is the last one in the last restaurant O(n^2).
There are a lot of possible solutions, many of which would require DS&A knowledge. Though the simplest one would be to use a Map data structure.
You can have a List of restaurants that contains a Map of items instead of a list. With this simple change, you can achieve O(n) complexity by only iterating through the restaurants.

How can I create a list with letters as headers?

I want to make a list of currencies in alphabetical order with letters as headers. Please have a look at the pictures for better understanding.
This is what I want to achieve
The list I have been able to achieve so far (A simple one)
I searched for packages but I couldn't find anything as per my need. Your help would be much appreciated.
Thanks.
Sounds like: how to implement Alphabet scroll in flutter
another options is: You can make a Map for example
countriesDict = {"a" : {"Algeria, Andorra", "b" : {"Bolivia, Bosnia ...."}}}
and retrieve it it in your code. For example:
for (country in countriesDict) {
... Your code to sort it out
}
and then display it.
You can use this package to implement your desired result easily.
Grouped List
Rest of it depends on your design.

Without an ID, how do you handle a dropdown that could have different values based on the language?

I'm used to web dev where a dropdown has an ID field along with the text. Is there a proper way to handle associating the correct drop down value when the text could change depending on the language?
Eg. Drop down could have English and Japanese or 英語 and
日本人. Depending on the local dialect.
In the web world, I would have ID 1 and 2 and the text wouldn't matter. There are some ways I can see getting around it, but hoping there is a cleaner way. Will be using in a number of places with some decently large lists at times.
I've watched a few localization tutorials, but none address the issues with dropdowns and localization text.
I could build a dictionary when creating the dropdown or do a couple queries (using Sqlite), but not really as efficient as just having a unique Id associated with an option.
I did it like this for our game. Not pretty, but it works.
var index = 0;
MapDropdown.AddOptions(MapConfigs.Instance.Configs.Select((c, i) => {
if(c.Id == config.Id) {
index = i;
}
return new Dropdown.OptionData { text = c.Name };
}).ToList());
MapDropdown.value = index;
MapChanged (index);
MapDropdown.onValueChanged.AddListener (MapChanged);

Autocomplete with Firebase

How does one use Firebase to do basic auto-completion/text preview?
For example, imagine a blog backed by Firebase where the blogger can tag posts with tags. As the blogger is tagging a new post, it would be helpful if they could see all currently-existing tags that matched the first few keystrokes they've entered. So if "blog," "black," "blazing saddles," and "bulldogs" were tags, if the user types "bl" they get the first three but not "bulldogs."
My initial thought was that we could set the tag with the priority of the tag, and use startAt, such that our query would look something like:
fb.child('tags').startAt('bl').limitToFirst(5).once('value', function(snap) {
console.log(snap.val())
});
But this would also return "bulldog" as one of the results (not the end of the world, but not the best either). Using startAt('bl').endAt('bl') returns no results. Is there another way to accomplish this?
(I know that one option is that this is something we could use a search server, like ElasticSearch, for -- see https://www.firebase.com/blog/2014-01-02-queries-part-two.html -- but I'd love to keep as much in Firebase as possible.)
Edit
As Kato suggested, here's a concrete example. We have 20,000 users, with their names stored as such:
/users/$userId/name
Oftentimes, users will be looking up another user by name. As a user is looking up their buddy, we'd like a drop-down to populate a list of users whose names start with the letters that the searcher has inputted. So if I typed in "Ja" I would expect to see "Jake Heller," "jake gyllenhaal," "Jack Donaghy," etc. in the drop-down.
I know this is an old topic, but it's still relevant. Based on Neil's answer above, you more easily search doing the following:
fb.child('tags').startAt(queryString).endAt(queryString + '\uf8ff').limit(5)
See Firebase Retrieving Data.
The \uf8ff character used in the query above is a very high code point
in the Unicode range. Because it is after most regular characters in
Unicode, the query matches all values that start with queryString.
As inspired by Kato's comments -- one way to approach this problem is to set the priority to the field you want to search on for your autocomplete and use startAt(), limit(), and client-side filtering to return only the results that you want. You'll want to make sure that the priority and the search term is lower-cased, since Firebase is case-sensitive.
This is a crude example to demonstrate this using the Users example I laid out in the question:
For a search for "ja", assuming all users have their priority set to the lowercased version of the user's name:
fb.child('users').
startAt('ja'). // The user-inputted search
limitToFirst(20).
once('value', function(snap) {
for(key in snap.val()){
if(snap.val()[key].indexOf('ja') === 0) {
console.log(snap.val()[key];
}
}
});
This should only return the names that actually begin with "ja" (even if Firebase actually returns names alphabetically after "ja").
I choose to use limitToFirst(20) to keep the response size small and because, realistically, you'll never need more than 20 for the autocomplete drop-down. There are probably better ways to do the filtering, but this should at least demonstrate the concept.
Hope this helps someone! And it's quite possible the Firebase guys have a better answer.
(Note that this is very limited -- if someone searches for the last name, it won't return what they're looking for. Hence the "best" answer is probably to use a search backend with something like Kato's Flashlight.)
It strikes me that there's a much simpler and more elegant way of achieving this than client side filtering or hacking Elastic.
By converting the search key into its' Unicode value and storing that as the priority, you can search by startAt() and endAt() by incrementing the value by one.
var start = "ABA";
var pad = "AAAAAAAAAA";
start += pad.substring(0, pad.length - start.length);
var blob = new Blob([start]);
var reader = new FileReader();
reader.onload = function(e) {
var typedArray = new Uint8Array(e.target.result);
var array = Array.prototype.slice.call(typedArray);
var priority = parseInt(array.join(""));
console.log("Priority of", start, "is:", priority);
}
reader.readAsArrayBuffer(blob);
You can then limit your search priority to the key "ABB" by incrementing the last charCode by one and doing the same conversion:
var limit = String.fromCharCode(start.charCodeAt(start.length -1) +1);
limit = start.substring(0, start.length -1) +limit;
"ABA..." to "ABB..." ends up with priorities of:
Start: 65666565656565650000
End: 65666665656565650000
Simples!
Based on Jake and Matt's answer, updated version for sdk 3.1. '.limit' no longer works:
firebaseDb.ref('users')
.orderByChild('name')
.startAt(query)
.endAt(`${query}\uf8ff`)
.limitToFirst(5)
.on('child_added', (child) => {
console.log(
{
id: child.key,
name: child.val().name
}
)
})

Blackberry, searching contacts from list

I want to add an Search Box in list Field. so that When i Enter a letter, then it will show the names starting with the letter 'A' , and so on. Iam using Vector to save the list of contacts same as the image shown :
If you want to select from the Contacts, use the ContactList.choose() method.
DO NOT try to iterate through the entire contacts your self every time. Remember there are lot of people having thousands of contacts and your code will be very unresponsive.
See: https://stackoverflow.com/a/4436816/371534
However, if you want to have 'filter as you type' kind of functionality with some other data, use the KeywordFilterField. You can get a sample code for it in the BlackBerry JDK samples.
Set a FieldChangeListener (or listen for alphanumeric key presses) to your EditField. Then refresh the list each time. Filtering on entries starting with the string contained in the EditField.
I wrote this on a pc without the Blackberry plugin installed, so couldn't test it, but it should be something like this.
String prefix = editField.getText();
Enumeration e = list.items();
while(e.hasMoreElements())
{
PIMItem item = (PIMItem) e.nextElement();
String name = item.getString(PIMItem.NAME,0);
if (name.startsWith(prefix))
{
//TODO display on screen
}
}