Home > Net >  I wants to filter list view as per the search query and there should be pagination used for 1000 doc
I wants to filter list view as per the search query and there should be pagination used for 1000 doc

Time:06-24

I am getting data of documents through API. I have tried many ways but couldn't resolved that how to filter the list.

CodePudding user response:

I didn't properly understand your question, but you can use the stream builder.

You get the pages from the api and add them to the stream. Check this link. https://api.flutter.dev/flutter/widgets/StreamBuilder-class.html

CodePudding user response:

you have to show some of the code you already have so as to know where to start from helping you, but anyways, this is the basic logic to do it,

first, you should have some sort of searchDelegate set up, then in your CustomSearchDelegate you should

class MyDelegate extends SearchDelegate<returnType> {
  @override
  Widget? buildLeading(BuildContext context) {
    return IconButton(
      icon: const Icon(Icons.arrow_back), // could be anything
      onPressed: () {
        Navigator.pop(context); /* could be anything you want your leading icon to do */
      },
    );
  }

  @override
  List<Widget>? buildActions(BuildContext context) {
    return [
      IconButton(
        icon: const Icon(Icons.clear),
        onPressed: () {
          query.isEmpty ? Navigator.pop(context) : query = ''; // again, could be anything at all
        },
      ),
    ];
  }

  @override
  Widget buildResults(BuildContext context) {
    return const Center();
  }

  @override
  Widget buildSuggestions(BuildContext context) {
    List<dataType> suggestions; 
    query.isEmpty
        ? suggestions = [] /* if you want the list to be empty first time user opens searchpage or if user clears search field else, remove the check */
        : suggestions = ListfromAPI.where((data) {
            final result = data.toLowerCase();
            final input = query.toLowerCase();
            return result.contains(input);
          }).toList();

    return ListView.builder(
        itemCount: suggestions.length,
        itemBuilder: (context, suggestIndex) {
          final suggestion = suggestions[suggestIndex];

          return ListTile(
            title: Text(suggestion),
            onTap: () {
              close(context, suggestion); /* passing back the data user selected and where you call showSearch(), you await this data and when it arrives, you use it to do anything you want to */
            },
          );
        });
  }
}
  • Related