Home > Software design >  Flutter search listview item
Flutter search listview item

Time:12-12

This is search address from cutnumid to show listview showing all address done and how can I want to search by typing only part of address? I've tried many different things and always crashes. please help me

//search custnum showaddress
 onSearch(String text) async {
if (text.isNotEmpty) {
  List<Item> itemList = [];
  for (var item in items) {
    if (item.custnum == text.toLowerCase().toUpperCase()) {
      itemList.add(item);
      setState(() {
        searchitems.clear();
        searchitems.addAll(itemList);
        print('name : ${searchitems[0].name}');
        if (searchitems.isEmpty) {
          searchitems = [];
          // print('searchitems : ${searchitems[0].address!.length}');
          // print('searchitems : ${searchitems[0].address!}');
        }
      });
    }
  }
} else {
  setState(() {
    searchitems.clear();

    // searchitems.addAll(items);
    print('searchitems : $searchitems');
  });
}

}

This is the code to search only for the words typed into the textfield. to display only searched This section may be invalid code. I gave an example.

onSearchAddr(String text) async {
if (text.isEmpty) {
  setState(() {
    loading = false;
  });
  return;
}

searchitems[0].address!.forEach((f) {
  if (f.addr1.toString().toLowerCase().contains(text))
    items.add(searchitems[0]);
});

}

CodePudding user response:

You setState() on every single iteration. What if the iteration is faster than build method? it will crash, cause the app has to be redrawn when it is still redrawing.

naive way to search by part of string. use contains method of String.

 onSearch(String text) async {
if (text.isNotEmpty) {
  List<Item> itemList = [];
  for (var item in items) {
    if (item.custnum.toLowerCase().contains(text.toLowerCase())) {
      itemList.add(item);
      
    }
  }
  setState(() {
        searchitems.clear();
        searchitems.addAll(itemList);
        print('name : ${searchitems[0].name}');
      });
} else {
  setState(() {
    searchitems.clear();

    // searchitems.addAll(items);
    print('searchitems : $searchitems');
  });
}

CodePudding user response:

The logic of your code seems to be working.

It could be errors from elsewhere such as unbounded ListView


class MyWidget extends StatefulWidget {
  const MyWidget({super.key});

  @override
  State<MyWidget> createState() => _MyWidgetState();
}

enum Item { one, two, three, four, ONE, TWO, THREE, FOUR }

class _MyWidgetState extends State<MyWidget> {
  //search custnum showaddress
  List<Item> searchitems = [Item.ONE];
  List<Item> items = Item.values;
  onSearch(String text) async {
    if (text.isNotEmpty) {
      List<Item> itemList = [];
      for (var item in items) {
        if (item.name.toLowerCase() == text) {
          itemList.add(item);
          setState(() {
            searchitems.clear();
            searchitems.addAll(itemList);
            print('name : ${searchitems[0].name}');
            if (searchitems.isEmpty) {
              searchitems = [];
              // print('searchitems : ${searchitems[0].address!.length}');
              // print('searchitems : ${searchitems[0].address!}');
            }
          });
        }
      }
    } else {
      setState(() {
        searchitems.clear();

        // searchitems.addAll(items);
        print('searchitems : $searchitems');
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        TextField(
          onSubmitted: onSearch,
        ),
        Expanded(
          child: ListView(
            children: searchitems.map((e) => Text(e.name)).toList(),
          ),
        )
      ],
    );
  }
}
  • Related