SearchDelegate does not call buildResults method

1,061

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."

Share:
1,061
Pritish
Author by

Pritish

Updated on December 04, 2022

Comments

  • Pritish
    Pritish over 1 year

    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

    • Pritish
      Pritish about 5 years
      @pskink you mean buildResults?
    • Pritish
      Pritish about 5 years
      @pskink do I need to edit the search.dart file?
    • Pritish
      Pritish about 5 years
      how am I supposed to know when showResults Is called as that function is defined in search.dart and I don't use it in my file
    • Pritish
      Pritish about 5 years
      @pskink I m not sure how adding break point will help, I didn't understood your comment of showResults called