I have a list view and an Appbar. The Appbar has a search icon on which I am calling the SearchDelegate. Following is my code for SearchDelegate

class StudentSearch extends SearchDelegate<StudentModel> {
  final Observable<StudentModel> studentModelDataList;

  StudentSearch(this.studentModelDataList);

  @override
  List<Widget> buildActions(BuildContext context) {
    return [
      IconButton(
          icon: Icon(Icons.clear),
          onPressed: () {
            query = "";
          }),
    ];
  }

  @override
  Widget buildLeading(BuildContext context) {
    return IconButton(
        icon: Icon(Icons.arrow_back),
        onPressed: () {
          close(context, null);
        });
  }

  @override
  Widget buildResults(BuildContext context) {
    return Text(query);
  }

  @override
  Widget buildSuggestions(BuildContext context) {
    return StreamBuilder(
        stream: studentModelDataList,
        builder: (context, AsyncSnapshot<StudentModel> snapshot) {
          if (!snapshot.hasData || snapshot.data == null) {
            return Center(
              child: CircularProgressIndicator(),
            );
          }

          final results = snapshot.data.studentModelData.studentData
              .where((a) => a.studName.toLowerCase().contains(query.toLowerCase()))
              .toList();

          if (snapshot.data != null) {
            return Refresh(
              year_id: "2",
              schoolId: "1",
              lastIndex: "0",
              disciplineId: "1",
              child: ListView.builder(
                itemBuilder: (context, int index) {
                  return ListTile(
                    onTap: (){
                      print(snapshot.data.studentModelData.studentData[index].studName);
query = snapshot.data.studentModelData.studentData[index].studName;
                          close(context, snapshot.data);

                    },
                    title: Text(results[index].studName),
                    subtitle: Text('${results[index].studentEmail} '),
                    trailing: Column(
                      children: [
                        Icon(Icons.comment),
                        Text('${results[index].classCode}'),
                      ],
                    ),
                  );
                },
                itemCount: results.length,
              ),
            );
          }
        });
  }
}

What I understand from the documentation is that whenever I enter an item to be searched, the SearchDelegate first shows buildSuggestions and then buildResults. So I am trying to search by a student name and the buildSuggestions method works properly but when I click on an item of buildSuggestions, it does not show me buildresults


Solution 1: DMonkey

BuildSuggestions is meant to do just that, build a list of suggestions to whatever is typed in the search bar. The listener responds to each and every change to the Text Field state. This could be used to provide autocomplete suggestions to the user.

BuildResults is called when the user calls for the results by clicking on the "magnifying glass" button on the keyboard, or whatever is used to "submit" the results indicating that the user has finished their typing and is requesting the results of the text search.

You can see an explanation here Flutter's Search Support (The Boring Flutter Development Show, Ep. 10)

Taken from the documentation for buildResults(BuildContext context) → Widget "The results shown after the user submits a search from the search page."


Solution 2: user12202271

Try Calling the buildResults function at the ontap() session in your ListTile widget. Also call the data you want displayed by the BuildResults inside it like this

     @override
     Widget buildResults(BuildContext context,["Data you want displayed when you click 
     search"]) {
     return Text(query);
      }

And in the suggestion area like this

 onTap: () =>

                         Navigator.push(
                              context,
                              MaterialPageRoute(
                                builder: (context) => Scaffold(
                                    appBar: AppBar(
                                        backgroundColor:
                                            Colors.blueGrey[900],
                                        title: new Text(
                                          "Country",
                                          style: TextStyle(
                                              color: Colors.white),
                                        )),
                                    body: buildResults(context)),
                              )),

Ofcourse this is just for an Example however you want to call the buildResults "lies in your court". This is how it works, The buildResult(context) takes the context in this case the data you are using from "final results" whether in "BuildResults(BuildContext, context)" and displays as called or used in-code like this

final results = snapshot.data.length;
            results = filteredUsers
                 .where((a) => (a.region.contains(query))
                      || a.region.contains(query)
                     )
                 .toList();

return ListView.builder(
                        itemCount: results.length,
                         itemBuilder: (context, index) => Column(
                           crossAxisAlignment: CrossAxisAlignment.center,
                           mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                           mainAxisSize: MainAxisSize.max,
                           children: <Widget>[
              //As used in-code
                          Text(${results[index].region})
                             ]))

So in my case "Data you want displayed when you click search"=[Results] represented like this

 @override
  Widget buildResults(BuildContext context, [results]) {
//How you want your BuildResult page to look like
}


Solution 3: Anandu Thampi

You can also use this code to call buildResult

onTap: () {
       showResults(context);
}