首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >颤动:按搜索时SearchDelegate流为空

颤动:按搜索时SearchDelegate流为空
EN

Stack Overflow用户
提问于 2020-02-05 02:16:06
回答 1查看 292关注 0票数 2

我在SearchDelegate中使用Stream

当我在搜索字段中输入一些东西时,它工作得很好。但只要我按下搜索符号,它就什么也不显示,因为流是空的。那件事怎么可能?

下面是一些代码:

代码语言:javascript
运行
复制
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){
              ....
   }
}

为什么调用buildResults()时流为空,而调用buildSuggestions()时流不为空?

EN

回答 1

Stack Overflow用户

发布于 2020-02-06 13:07:30

在抽象类SearchDelegate:中编辑

代码语言:javascript
运行
复制
void showResults(BuildContext context) {
    _focusNode?.unfocus();
    _currentBody = _SearchBody.results;
 }

替换

代码语言:javascript
运行
复制
void showResults(BuildContext context) {
    if(_queryTextController.text.isNotEmpty) {
      _focusNode?.unfocus();
      _currentBody = _SearchBody.results;
    }else{
      _focusNode?.unfocus();
    }
  }

这对我很管用

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60063341

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档