Firebase 颤振搜索代理仅显示一次结果和建议

Firebase 颤振搜索代理仅显示一次结果和建议,firebase,flutter,google-cloud-firestore,Firebase,Flutter,Google Cloud Firestore,我正在使用SearchDelegate实现颤振的搜索功能。我已经组织了我的代码,类似于来自的Flutter开发人员。当点击搜索栏并第一次打开时,程序运行正常,我可以看到建议,这些建议是使用buildSuggestions构建的。稍后,当我按下搜索图标时,为了查看搜索结果,我没有看到任何数据,因为出于某种原因,我在搜索时用作数据源的流没有返回任何数据。然后,当我第二次在搜索字段中输入更多文本时,我看不到任何建议 我试图用这样一个简单的结构来代替构建建议: @override Widget

我正在使用SearchDelegate实现颤振的搜索功能。我已经组织了我的代码,类似于来自的Flutter开发人员。当点击搜索栏并第一次打开时,程序运行正常,我可以看到建议,这些建议是使用buildSuggestions构建的。稍后,当我按下搜索图标时,为了查看搜索结果,我没有看到任何数据,因为出于某种原因,我在搜索时用作数据源的流没有返回任何数据。然后,当我第二次在搜索字段中输入更多文本时,我看不到任何建议

我试图用这样一个简单的结构来代替构建建议:

  @override
  Widget buildSuggestions(BuildContext context) {
     Container();
   }
之后,当我第一次点击搜索图标时,buildResult就开始工作了。之后,buildSuggestions和buildResult都返回null(=不工作)

我认为,我正在收听的流在构建搜索时出现了一些问题,因为当我执行第一个buildSuggestion或第一个buildResults时,它会返回一些内容,但在执行之后会停止工作。我已经尝试使用.asBroadcastStream(),但没有帮助。希望有人能帮我解决这个问题。以下是showSearch的调用:

Container(
  child: InkWell (
     onTap: () async {
                            var str = repo.streamUsers().asBroadcastStream();
                            await showSearch(
                              context: context,
                              delegate: GroupieSearch(
                                str,
                              ),
                            );
                          }
                        },
),
);
以下是SearchDelegate:

class GroupieSearch extends SearchDelegate<UserModel> {
  final Stream<List<UserModel>> users;

  GroupieSearch(this.users);

  @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 StreamBuilder<List<UserModel>>(
        stream: users,
        builder: (context, AsyncSnapshot<List<UserModel>> snapshot) {
          if (!snapshot.hasData) {
            return Center(
              child: Text('No results!'),
            );
          }

          final result = snapshot.data
              .where((a) => a.userName.toLowerCase().contains(query));

          return Container(
            // color: Color.fromRGBO(45, 0, 61, 1),
            color: Color.fromRGBO(210, 176, 209, 1),
            child: ListView(
              children: result
                  .map<ListTile>((a) => ListTile(
                        title: Text(
                          a.userName,
                          style: TextStyle(
                            color: Colors.white,
                            fontSize: 20,
                            fontFamily: "Berlin Sans FB",
                            fontWeight: FontWeight.w500,
                          ),
                        ),
                        leading: new Container(
                          width: 52.0,
                          height: 52.0,
                          decoration: new BoxDecoration(
                            color: Color.fromRGBO(210, 176, 209, 1),
                            shape: BoxShape.circle,
                            image: new DecorationImage(
                              fit: BoxFit.cover,
                              image: NetworkImage(a.image),
                            ),
                          ),
                        ),
                        subtitle: Text(
                          a.fullName,
                          style: TextStyle(
                            color: Colors.white.withOpacity(0.8),
                            fontSize: 14,
                            fontFamily: "Berlin Sans FB",
                            fontWeight: FontWeight.w400,
                          ),
                        ),
                        onTap: () {
                          close(context, a);
                        },
                      ))
                  .toList(),
            ),
          );
        });
  }

  @override
  Widget buildSuggestions(BuildContext context) {
    return StreamBuilder<List<UserModel>>(
        stream: users,
        builder: (context, AsyncSnapshot<List<UserModel>> snapshot) {
          if (!snapshot.hasData) {
            return Center(
              child: Text('No suggestions!'),
            );
          }

          final result = snapshot.data
              .where((a) => a.userName.toLowerCase().contains(query));

          return Container(
            // color: Color.fromRGBO(45, 0, 61, 1),
            color: Color.fromRGBO(210, 176, 209, 1),
            child: ListView(
              children: result
                  .map<ListTile>((a) => ListTile(
                        title: Text(
                          a.userName,
                          style: TextStyle(
                            color: Colors.white,
                            fontSize: 20,
                            fontFamily: "Berlin Sans FB",
                            fontWeight: FontWeight.w500,
                          ),
                        ),
                        leading: new Container(
                          width: 52.0,
                          height: 52.0,
                          decoration: new BoxDecoration(
                            color: Color.fromRGBO(210, 176, 209, 1),
                            shape: BoxShape.circle,
                            image: new DecorationImage(
                              fit: BoxFit.cover,
                              image: NetworkImage(a.image),
                            ),
                          ),
                        ),
                        subtitle: Text(
                          a.fullName,
                          style: TextStyle(
                            color: Colors.white.withOpacity(0.8),
                            fontSize: 14,
                            fontFamily: "Berlin Sans FB",
                            fontWeight: FontWeight.w400,
                          ),
                        ),
                        onTap: () {
                          close(context, a);
                          // query = a.userName;
                        },
                      ))
                  .toList(),
            ),
          );
        });
  }
class GroupieSearch扩展了SearchDelegate{
最终流用户;
GroupieSearch(this.users);
@凌驾
列出buildActions(BuildContext上下文){
返回[
图标按钮(
图标:图标(图标。清除),
已按下:(){
查询=“”;
},
),
];
}
@凌驾
小部件buildLeading(BuildContext上下文){
返回图标按钮(
图标:图标(图标。箭头返回),
已按下:(){
关闭(上下文,空);
},
);
}
@凌驾
小部件构建结果(构建上下文){
返回流生成器(
流:用户,
生成器:(上下文,异步快照){
如果(!snapshot.hasData){
返回中心(
子项:文本('无结果!'),
);
}
最终结果=snapshot.data
。其中((a)=>a.userName.toLowerCase()包含(查询));
返回容器(
//颜色:color.fromRGBO(45,0,61,1),
颜色:颜色。来自RGBO(2101762091),
子:ListView(
儿童:结果
.map((a)=>ListTile(
标题:正文(
a、 用户名,
样式:TextStyle(
颜色:颜色,白色,
尺寸:20,
fontFamily:“柏林无FB”,
fontWeight:fontWeight.w500,
),
),
领先:新集装箱(
宽度:52.0,
身高:52.0,
装饰:新盒子装饰(
颜色:颜色。来自RGBO(2101762091),
形状:BoxShape.circle,
图片:新装饰图片(
适合:BoxFit.cover,
图像:网络图像(a.image),
),
),
),
字幕:文本(
a、 全名,
样式:TextStyle(
颜色:颜色。白色。不透明度(0.8),
尺寸:14,
fontFamily:“柏林无FB”,
fontWeight:fontWeight.w400,
),
),
onTap:(){
关闭(上下文,a);
},
))
.toList(),
),
);
});
}
@凌驾
小部件构建建议(构建上下文){
返回流生成器(
流:用户,
生成器:(上下文,异步快照){
如果(!snapshot.hasData){
返回中心(
child:Text('无建议!'),
);
}
最终结果=snapshot.data
。其中((a)=>a.userName.toLowerCase()包含(查询));
返回容器(
//颜色:color.fromRGBO(45,0,61,1),
颜色:颜色。来自RGBO(2101762091),
子:ListView(
儿童:结果
.map((a)=>ListTile(
标题:正文(
a、 用户名,
样式:TextStyle(
颜色:颜色,白色,
尺寸:20,
fontFamily:“柏林无FB”,
fontWeight:fontWeight.w500,
),
),
领先:新集装箱(
宽度:52.0,
身高:52.0,
装饰:新盒子装饰(
颜色:颜色。来自RGBO(2101762091),
形状:BoxShape.circle,
图片:新装饰图片(
适合:BoxFit.cover,
图像:网络图像(a.image),
),
),
),
字幕:文本(
a、 全名,
样式:TextStyle(
颜色:颜色。白色。不透明度(0.8),
尺寸:14,
fontFamily:“柏林无FB”,
fontWeight:fontWeight.w400,
),
),
onTap:(){
class UserRepository {
  final userCollection = Firestore().collection("usersFreeData");

  Stream<List<UserModel>> streamUsers() {
    print('START STREAM HARDCODED USERS');
    try {
      print("+     +     +     +     start searching users");
      return userCollection.snapshots().map((list) {
        return list.documents.map((doc) {
          print("doc.data : ${doc.data}");
          print("doc.documentID : ${doc.documentID}");
          print("doc.hashCode : ${doc.hashCode}");

          Map data = doc.data;
          return UserModel(
            documentID: doc.documentID,
            image: data["image"],
            fullName: data["fullName"],
            userName: data["userName"],
            description: data["description"],
            friends: data["friends"],
          );
        }).toList(); //.toList()
      }).asBroadcastStream();
    } on PlatformException catch (e) {
      print(e);
      return null;
    } catch (err) {
      print(err);
      return null;
    }
  }
}