[SOLVED] Search array for listview filtering – Dart/Flutter


This Content is from Stack Overflow. Question asked by Mark McKeon

I am trying to integrate a search and filter textfield in my app, but I am not completely sure of the errors my compiler is throwing. I think I am having type issues, or that I have not set up my inheritances correctly?
Ok, so basically I am sending a http request to an api and receiving this data where I have set up my class to display it. In a futurebuilder I have some conditional logic trying to sort the right data I need into separate lists(Im not sure if this the right thing to do but I digress) I then create a map (resultMap) and within that I loop through the arrays and nest a map (innerMap).

     class _HomePageState extends State<HomePage> {
      final controller = TextEditingController();
      String? searchString = '';
      late final Future<ProjectModel> futureProjects;

      List<ProjectSearch> searchList = []; //I think this causing this issues.....
      List<String?> jobNames = [];
      List<String?> jobNumbers = [];
      List<String?> techs = [];
      List<String?> pms = [];
      List<String?> address = [];
      List<String?> majors = [];
      List<String?> budget = [];
      Map resultMap = {};

       void initState() {
       futureProjects = fetchProjects();

      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
           title: const Text(
           'Upcoming / Live Projects',
           style: TextStyle(
            color: Colors.white,
            fontSize: 18,
            fontWeight: FontWeight.bold,
        backgroundColor: ColorConstants.darkScaffoldBackgroundColor,
      drawer: const CustomDrawer(),
      backgroundColor: ColorConstants.lightScaffoldBackgroundColor,

      // ignore: avoid_unnecessary_containers
      body: Center(
        child: FutureBuilder<ProjectModel>(
            future: futureProjects,
            builder: (context, snapshot) {
              if (snapshot.hasData) {
                var data = snapshot.data!;
                var columns = data.columns;
                var rows = data.rows;

                for (var item in rows!) {
                  var cells = item.cells;
                  for (var elements in cells!) 
                    if (elements.columnId != null) {
                      if (elements.columnId == 2057691532158852) {
                        var displayValues = elements.displayValue;
                        if (displayValues != null) {
                       ..... continuing conditional logic

     //Here is where I nest my arrays to the resultMap map:
                int index = 0;
                for (int i = 0; i < jobNames.length; i++) {
                  Map innerMap = {};

                  innerMap['name'] = jobNames[index];
                  innerMap['address'] = address[index];
                  innerMap['jobNumber'] = jobNumbers[index];
                  innerMap['major'] = majors[index];
                  innerMap['tech'] = techs[index];
                  innerMap['pM'] = pms[index];
                  innerMap['budget'] = budget[index];

                  resultMap[i] = innerMap;

I have been looking trough some tutorials and it looks like it is easier to apply string search functions using .contains() but these only work on lists. So I convert my resultMap using a forEach to create my searchList list.

                resultMap.forEach((key, value) => searchList.add(value));

Here is my TextFormField:

                          textAlign: TextAlign.center,
                          controller: controller,
                          onChanged: search,

And then my ListView.builder:

                    return MaterialButton(
                      onPressed: () => showModalBottomSheet<void>(
                      backgroundColor: Colors.transparent,
                     context: context,
                      builder: (BuildContext context) {
                              final projectData = searchList[index];

And finally the function I am using to search:

     void search(String query) {
       List finder = searchList;

        final suggestions = searchList.where((check) {
        final projectName = check['name'].toLowerCase();
        final input = query.toLowerCase();

         return projectName.contains(input);

          setState(() {
           finder = suggestions;

I hope this makes sense…
When I get rid of the model it doesn’t break down but the search function doesnt filter thru the array. However here is the compile error:

    type 'List<dynamic>' is not a subtype of type 'List<ProjectModel>' of 'function 


Try this showSearch() method

It is an inbuilt functionality which is simple to use.

This Question was asked in StackOverflow by Mark McKeon and Answered by Dinesh It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?