Flutter search nested list of objects

Viewed 35

I am building a Flutter app for selling alcohol/bottle delivery service. I'm having issues trying to search a beverage in the search bar. I have put the bottles in a nested list because the bottles have different categories. When I try to search for a bottle nothing actually comes up in the search bar. I guess I have something to fix in my code. I define the nested list categories. Then I access the bottle object using the method getBottle() method and return a list of bottle objects which is assigned to a new list called allBottles. The retrieved bottle object is then used in a search query. However I get nothing when I type in the searchbar. Can anyone please assist. Below is my code

This image shows the search bar with the hintext

enter image description here

This image shows the search bar with the searched text

enter image description here

class ProductProvider with ChangeNotifier {
  final List<BottleCategory> categories = [
    BottleCategory(categoryNo: 1, bottleType: 'Beers', bottleList: [
      Bottle(
          id: 'AMST',
          bottleName: 'Amstel Lager',
          image: 'lib/assets/images/AmstelLager.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'BLKL',
          bottleName: 'Black Label',
          image: 'lib/assets/images/BlackLabel.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'CSTL',
          bottleName: 'Castle Lite',
          image: 'lib/assets/images/CastleLite.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'CRNE',
          bottleName: 'Corona Extra',
          image: 'lib/assets/images/CoronaExtra.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'HNKN',
          bottleName: 'Heineken',
          image: 'lib/assets/images/HeinekenOriginalBottle.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'WDHK',
          bottleName: 'Windhoek Draught',
          image: 'lib/assets/images/WindhoekDraught.png',
          time: 20,
          price: 274.00),
    ]),
    BottleCategory(categoryNo: 2, bottleType: 'Brandy', bottleList: [
      Bottle(
          id: 'KLPB',
          bottleName: 'Klipdrift Brandy',
          image: 'lib/assets/images/KlipdriftBrandy.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'KWV3',
          bottleName: 'Kwv Brandy 3 years',
          image: 'lib/assets/images/KwvBrandy3years.jpg',
          time: 20,
          price: 274.00),
    ]),
    BottleCategory(categoryNo: 3, bottleType: 'Ciders', bottleList: [
      Bottle(
          id: 'BLGT',
          bottleName: 'Belgravia Gin and Tonic',
          image: 'lib/assets/images/BelgraviaGinAndTonic.jpg',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'BRNB',
          bottleName: 'Bernini Blush',
          image: 'lib/assets/images/BerniniBlush.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'BRTF',
          bottleName: 'Brutal Fruit',
          image: 'lib/assets/images/BrutalFruit.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'FLYF',
          bottleName: 'Flying Fish',
          image: 'lib/assets/images/FlyingFish.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'HNTD',
          bottleName: 'Hunters Dry',
          image: 'lib/assets/images/HuntersDry.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'HNTG',
          bottleName: 'Hunters Gold',
          image: 'lib/assets/images/HuntersGold.jpg',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'ICET',
          bottleName: 'Ice Tropez',
          image: 'lib/assets/images/IceTropez.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'RDSB',
          bottleName: 'Red Square Blue Ice',
          image: 'lib/assets/images/RedSquareBlueIce.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'SRMG',
          bottleName: 'Smirnoff Guarana',
          image: 'lib/assets/images/SmirnoffGuarana.jpg',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'SRMS',
          bottleName: 'Smirnoff Storm',
          image: 'lib/assets/images/SmirnoffStorm.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'STRB',
          bottleName: 'Strong bow',
          image: 'lib/assets/images/Strongbow.png',
          time: 20,
          price: 274.00),
    ]),
    BottleCategory(categoryNo: 4, bottleType: 'Cocktails', bottleList: [
      Bottle(
          id: '',
          bottleName: 'Heineken',
          image: 'lib/assets/images/HeinekenOriginalBottle.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: '',
          bottleName: 'Heineken',
          image: 'lib/assets/images/HeinekenOriginalBottle.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: '',
          bottleName: 'Heineken',
          image: 'lib/assets/images/HeinekenOriginalBottle.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: '',
          bottleName: 'Heineken',
          image: 'lib/assets/images/HeinekenOriginalBottle.png',
          time: 20,
          price: 274.00),
    ]),
    BottleCategory(categoryNo: 5, bottleType: 'Cognac', bottleList: [
      Bottle(
          id: 'CRVV',
          bottleName: 'Courvoisier VS',
          image: 'lib/assets/images/CourvoisierVS.jpg',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'HNSY',
          bottleName: 'Hennesy',
          image: 'lib/assets/images/HennesyCognac.jpeg',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'HNVC',
          bottleName: 'Honor VS Cognac',
          image: 'lib/assets/images/HonorVSCognac.jpg',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'RMVS',
          bottleName: 'Remy Martin VSOP Cognac',
          image: 'lib/assets/images/RemyMartinVSOPCognac.jpg',
          time: 20,
          price: 274.00),
    ]),
    BottleCategory(categoryNo: 6, bottleType: 'Dry Red Wine', bottleList: [
      Bottle(
          id: '',
          bottleName: 'Heineken',
          image: 'lib/assets/images/HeinekenOriginalBottle.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: '',
          bottleName: 'Heineken',
          image: 'lib/assets/images/HeinekenOriginalBottle.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: '',
          bottleName: 'Heineken',
          image: 'lib/assets/images/HeinekenOriginalBottle.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: '',
          bottleName: 'Heineken',
          image: 'lib/assets/images/HeinekenOriginalBottle.png',
          time: 20,
          price: 274.00),
    ]),
    BottleCategory(categoryNo: 7, bottleType: 'Gin', bottleList: [
      Bottle(
          id: 'BLGN',
          bottleName: 'Belgravia Gin',
          image: 'lib/assets/images/BelgraviaGin.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'BMBS',
          bottleName: 'Bombay Sapphire Gin',
          image: 'lib/assets/images/BombaySapphireGin.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'GDLG',
          bottleName: 'Gordon London Dry Gin',
          image: 'lib/assets/images/GordonLondonDryGin.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'TNGN',
          bottleName: 'Tanqueray Gin',
          image: 'lib/assets/images/TanquerayGin.png',
          time: 20,
          price: 274.00),
    ]),
    BottleCategory(categoryNo: 8, bottleType: 'Liqueur', bottleList: [
      Bottle(
          id: 'AMLC',
          bottleName: 'Amarula Cream',
          image: 'lib/assets/images/AmarulaCream.jpg',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'JAGM',
          bottleName: 'Jagermeister',
          image: 'lib/assets/images/Jagermeister.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'STWL',
          bottleName: 'Strawberry Lips',
          image: 'lib/assets/images/StrawberryLips.png',
          time: 20,
          price: 274.00),
    ]),
    BottleCategory(categoryNo: 9, bottleType: 'Soft Drinks', bottleList: [
      Bottle(
          id: 'APPT',
          bottleName: 'Appletiser',
          image: 'lib/assets/images/Appletiser.jpg',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'COCA',
          bottleName: 'Coca Cola',
          image: 'lib/assets/images/CocaCola.jpg',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'GRPT',
          bottleName: 'Grapetiser',
          image: 'lib/assets/images/Grapetiser.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'SPRT',
          bottleName: 'Sprite',
          image: 'lib/assets/images/Sprite.jpg',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'TNWT',
          bottleName: 'Tonic Water',
          image: 'lib/assets/images/TonicWater.png',
          time: 20,
          price: 274.00),
    ]),
    BottleCategory(categoryNo: 10, bottleType: 'Sparkling', bottleList: [
      Bottle(
          id: 'ARMG',
          bottleName: 'Armand De Brignac Brut Gold',
          image: 'lib/assets/images/ArmandDeBrignacBrutGold.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'DMPL',
          bottleName: 'Dom Perignon Luminous',
          image: 'lib/assets/images/DomPerignonLuminous.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'MCBI',
          bottleName: 'Moet and Chandon Brut Imperial',
          image: 'lib/assets/images/MoetAndChandonBrutImperial.jpg',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'MCNI',
          bottleName: 'Moet and Chandon Nectar Imperial',
          image: 'lib/assets/images/MoetAndChandonNectarImperial.jpg',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'MNIR',
          bottleName: 'Moet and Chandon Nectar Imperial Rose',
          image: 'lib/assets/images/MoetAndChandonNectarImperialRose.jpg',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'VVCB',
          bottleName: 'Veuve Clicquot Brut',
          image: 'lib/assets/images/VeuveClicquotBrut.jpg',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'VVCR',
          bottleName: 'Veuve Clicquot Rich',
          image: 'lib/assets/images/VeuveClicquotRich.jpg',
          time: 20,
          price: 274.00)
    ]),
    BottleCategory(categoryNo: 11, bottleType: 'Vodka', bottleList: [
      Bottle(
          id: '1818',
          bottleName: '1818 Vodka',
          image: 'lib/assets/images/1818-original.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'ABSL',
          bottleName: 'Absolut Vodka',
          image: 'lib/assets/images/AbsolutVodka.jpg',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'BLVD',
          bottleName: 'Belvedere Vodka',
          image: 'lib/assets/images/BelvedereVodka.jpg',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'CRCV',
          bottleName: 'Ciroc Vodka',
          image: 'lib/assets/images/CirocVodka.jpg',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'CRZB',
          bottleName: 'Cruz Banana',
          image: 'lib/assets/images/CruzBanana.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'CRZV',
          bottleName: 'Cruz Vintage',
          image: 'lib/assets/images/CruzVintage.jpg',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'CRZW',
          bottleName: 'Cruz Watermelon',
          image: 'lib/assets/images/CruzWatermelon.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'RDSV',
          bottleName: 'Red Square Vodka',
          image: 'lib/assets/images/RedSquareVodka.jpg',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'RUSB',
          bottleName: 'Russian Bear Vodka',
          image: 'lib/assets/images/RussianBearVodka.jpg',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'SKYY',
          bottleName: 'Skyy Vodka',
          image: 'lib/assets/images/SkyyVodka.png',
          time: 20,
          price: 274.00),
    ]),
    BottleCategory(categoryNo: 12, bottleType: 'White Wine', bottleList: [
      Bottle(
          id: '',
          bottleName: 'Heineken',
          image: 'lib/assets/images/HeinekenOriginalBottle.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: '',
          bottleName: 'Heineken',
          image: 'lib/assets/images/HeinekenOriginalBottle.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: '',
          bottleName: 'Heineken',
          image: 'lib/assets/images/HeinekenOriginalBottle.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: '',
          bottleName: 'Heineken',
          image: 'lib/assets/images/HeinekenOriginalBottle.png',
          time: 20,
          price: 274.00),
    ]),
    BottleCategory(categoryNo: 13, bottleType: 'Whiskey', bottleList: [
      Bottle(
          id: '',
          bottleName: 'Bells Scotch',
          image: 'lib/assets/images/BellsScotch.jpg',
          time: 20,
          price: 274.00),
      Bottle(
          id: '',
          bottleName: 'J & B Whiskey',
          image: 'lib/assets/images/JBWhiskey.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: '',
          bottleName: 'Jameson',
          image: 'lib/assets/images/Jameson.jpg',
          time: 20,
          price: 274.00),
      Bottle(
          id: '',
          bottleName: 'Johnnie Walker Black Label',
          image: 'lib/assets/images/JohnnieWalkerBlackLabel.jpg',
          time: 20,
          price: 274.00),
    ]),
  ];
  List<Bottle> getBottleItem() {
    List<Bottle> localBottles = [];
    for (var x = 0; x < categories.length; x++) {
      var currentElement = categories[x].bottleList;
      for (var y = 0; y < currentElement.length; y++) {
        var bottleElement = currentElement[y];
        localBottles.add(bottleElement);
      }
    }
    print(localBottles);
    return localBottles;
  }

  late List<Bottle> allBottles = getBottleItem();
  late List<Bottle> bottles;
  String query = " ";

  void searchBottle(String query) {
    final bottles = allBottles.where((bottle) {
      final titleLower = bottle.bottleName.toLowerCase();
      final searchLower = query.toLowerCase();

      return titleLower.contains(searchLower);
    }).toList();

    setState(() {
      this.bottles = bottles;
      this.query = query;
    });
  }

  Widget buildSearch() {
    return SearchWidget(
      text: query,
      hintText: 'Search for beverages',
      onChanged: searchBottle,
    );
  }
}

Code for searchWidget:

class SearchWidget extends StatefulWidget {
  final String text;
  final ValueChanged<String> onChanged;
  final String hintText;
  const SearchWidget(
      {Key? key,
      required this.text,
      required this.onChanged,
      required this.hintText})
      : super(key: key);

  @override
  State<SearchWidget> createState() => _SearchWidgetState();
}

class _SearchWidgetState extends State<SearchWidget> {
  final controller = TextEditingController();
  @override
  Widget build(BuildContext context) {
    return Container(
      child: TextField(
        controller: controller,
        decoration: InputDecoration(
            icon: const Icon(
              Icons.search,
              color: Colors.grey,
            ),
            suffixIcon: widget.text.isNotEmpty
                ? GestureDetector(
                    child: const Icon(Icons.close),
                    onTap: () {
                      controller.clear();
                      widget.onChanged('');
                      FocusScope.of(context).requestFocus(FocusNode());
                    },
                  )
                : null,
            hintText: widget.hintText,
            border: InputBorder.none),
      ),
    );
  }
}
1 Answers

You have to call your call onChanged, your onChanged in the TextField is not defined.

onChanged: widget.onChanged,
Related