Search code examples
flutterdartsearchbar

search bar I can't find anything


I'm new to coding and I've been trying to code the search bar for a while now but I've found that my search bar has a problem where it doesn't search what I want, it seems like it can't find anything. I tried to fix it for a while but still got the same problem. I studied from different websites. and then fix it, still stuck on the same problem, I think maybe I wrote the code in a wrong place but my code doesn't have any error.enter image description here

class view_problem extends StatefulWidget {
   @override
  State<view_problem> createState() => _view_problemState();
}

class _view_problemState extends State<view_problem> {
  TextEditingController _searchController = TextEditingController();

  List<problemModel> problemlist = [];
  StreamController _streamController = StreamController();
  Future getAllProblem() async {
    problemlist = await problemcontrollers().getProblem();
    _streamController.sink.add(problemlist);
  }

  @override
  void initState() {
    // TODO: implement initState
    Timer.periodic(Duration(seconds: 1), (timer) {
      getAllProblem();
    });
    super.initState();
  }

  void _filterSearch(String query) {
    List<problemModel> filteredList = [];
    for (var item in problemlist) {
      if (item.toString().contains(query.toString())) {
        filteredList.add(item);
      }
    }
    setState(() {
      problemlist = filteredList;
    });
  }

   @override
   Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Color.fromARGB(255, 14, 12, 134),
        title: const Text('show information'),
      ),
      body: Container(
        child: Column(
          children: <Widget>[
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: TextField(
                onChanged: (value) {
                  _filterSearch(value);
                },
                controller: _searchController,
                decoration: InputDecoration(
                    labelText: "search",
                    prefixIcon: Icon(Icons.search),
                    border: OutlineInputBorder(
                        borderRadius: BorderRadius.all(Radius.circular(25.0)))),
              ),
            ),
            Expanded(
              child: StreamBuilder(
                stream: _streamController.stream,
                builder: (context, snapshots) {
                  if (snapshots.hasData) {
                    return ListView.builder(
                        itemCount: problemlist.length,
                        itemBuilder: ((context, index) {
                           problemModel problem = problemlist[index];
                          return Card(
                            margin: EdgeInsets.all(10),
                            child: ListTile(
                              title: Text(
                                problem.name_surname,
                                style: TextStyle(
                                    fontWeight: FontWeight.bold, fontSize: 19),
                              ),
                              subtitle: Text(
                                problem.area,
                                style: TextStyle(
                                    fontWeight: FontWeight.bold, fontSize: 17),
                              ),
                              trailing: Text(
                                 problem.status,
                                style: TextStyle(
                                    fontWeight: FontWeight.bold, fontSize: 16),
                              ),
                            ),
                          );
                        }));
                  }
                  return Center(
                    child: CircularProgressIndicator(),
                  );
                },
              ),
            )
          ],
        ),
       ),
     );
  }
}

Solution

  • You have a timer running periodically running every second forever that gets all problems, which will overwrite any filtering. I don't think you want that timer there, and just do getAllProblem(); once in the initState. And save the originalList in another variable. Something like this

    class _view_problemState extends State<view_problem> {
      TextEditingController _searchController = TextEditingController();
    
      List<problemModel> problemlist = [];
      List<problemModel> originalList = [];
      StreamController _streamController = StreamController();
      Future getAllProblem() async {
        problemlist = await problemcontrollers().getProblem();
        originalList = problemlist;
        _streamController.sink.add(problemlist);
      }
    
      @override
      void initState() {
        getAllProblem();
        super.initState();
      }
    
      void _filterSearch(String query) {
        List<problemModel> filteredList = [];
        for (var item in originalList) {
          if (item.toString().contains(query.toString())) {
            filteredList.add(item);
          }
        }
        setState(() {
          problemlist = filteredList;
        });
      }
    
       @override
       Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            backgroundColor: Color.fromARGB(255, 14, 12, 134),
            title: const Text('show information'),
          ),
          body: Container(
            child: Column(
              children: <Widget>[
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: TextField(
                    onChanged: (value) {
                      _filterSearch(value);
                    },
                    controller: _searchController,
                    decoration: InputDecoration(
                        labelText: "search",
                        prefixIcon: Icon(Icons.search),
                        border: OutlineInputBorder(
                            borderRadius: BorderRadius.all(Radius.circular(25.0)))),
                  ),
                ),
                Expanded(
                  child: StreamBuilder(
                    stream: _streamController.stream,
                    builder: (context, snapshots) {
                      if (snapshots.hasData) {
                        return ListView.builder(
                            itemCount: problemlist.length,
                            itemBuilder: ((context, index) {
                               problemModel problem = problemlist[index];
                              return Card(
                                margin: EdgeInsets.all(10),
                                child: ListTile(
                                  title: Text(
                                    problem.name_surname,
                                    style: TextStyle(
                                        fontWeight: FontWeight.bold, fontSize: 19),
                                  ),
                                  subtitle: Text(
                                    problem.area,
                                    style: TextStyle(
                                        fontWeight: FontWeight.bold, fontSize: 17),
                                  ),
                                  trailing: Text(
                                     problem.status,
                                    style: TextStyle(
                                        fontWeight: FontWeight.bold, fontSize: 16),
                                  ),
                                ),
                              );
                            }));
                      }
                      return Center(
                        child: CircularProgressIndicator(),
                      );
                    },
                  ),
                )
              ],
            ),
           ),
         );
      }
    }