简体   繁体   中英

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

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.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM