Flutter: SearchDelegate Stream is empty when pressing Search

352

Edit in the abstract class SearchDelegate:

void showResults(BuildContext context) {
    _focusNode?.unfocus();
    _currentBody = _SearchBody.results;
 }

Replace:

void showResults(BuildContext context) {
    if(_queryTextController.text.isNotEmpty) {
      _focusNode?.unfocus();
      _currentBody = _SearchBody.results;
    }else{
      _focusNode?.unfocus();
    }
  }

It worked for me

Share:
352
Frederik
Author by

Frederik

Updated on December 17, 2022

Comments

  • Frederik
    Frederik over 1 year

    I‘m using a Stream with SearchDelegate.
    When I enter something in the search field, it's working fine. But as soon as I press the search symbol, it displays nothing, because the stream is empty. How is that possible?

    Here is some code:

    List<Notes> notes;
    
    ...
    
    void search() {
       showSearch(
         context: context,
         delegate: Search(
             Stream.value(UnmodifiableListView<Note>(notes)).asBroadcastStream()
         )
    }
    
    
    class Search extends SearchDelegate{
    
       final Stream<UnmodifiableListView<Note>> notes;
    
       Search(this.notes);
    
       ...
    
       @override
       Widget buildResults(BuildContext context){
          return _buildStreamBuilder();
       }
    
    
       @override
       Widget buildSuggestions(BuildContext context){
          return _buildStreamBuilder();
       }
    
    
       StreamBuilder<UnmodifiableListView<Note>> _buildStreamBuilder() {
         return StreamBuilder< UnmodifiableListView<Note>> (
           stream: notes
           builder: (context, AsyncSnapshot< UnmodifiableListView<Note>>) {
              final results = snapshot.data.where((note){
                  ....
       }
    }
    

    Why is the stream empty, when buildResults() is called, but not when buildSuggestions() is called?