How to implement a search bar in flutter E-commerce app with tons of products?

479

here's a package you can use Flutter Search Bar, personally I just use a second List and this simple function:

final List<String> filteredNames;
final List<String> names = [
  //your data or database
];

onChanged: (input) {
  input = input.toLowerCase(); //for no case-sensitive search
    setState(() {
      filteredNames = names.where((name) {
        var exName = name.toLowerCase();
        return exName.contains(input);
      }).toList();
   });
},

It's easy to implement but the downside is bad performance for very large lists (a few thousands should be working fine I think, try it out).

For handling search in really large lists check out Stack Overflow-How to handle searching large lists in Flutter?

Share:
479
Dhinesh
Author by

Dhinesh

Updated on December 25, 2022

Comments

  • Dhinesh
    Dhinesh over 1 year

    I've been developing an E-commerce app. It has a lot of products as of now and more are being added every day. I'm thinking of making a list of thousands of products (all of them) and do a linear search. Is there a better way to implement it. Suggest me some best practices. Thanks.

    • rstrelba
      rstrelba over 3 years
      Use elasticsearch on backend and paging in client app with ListView
  • Nuqo
    Nuqo over 3 years
    please mark the answer as accepted if it helped you 😊