简体   繁体   English

在 flutter 中为 firestore 列表执行搜索文本时出错

[英]error while imlpementing search text for the firestore list in flutter

I am implementing the below code for the search operation in the list in flutter application for firestore database.我正在为firestore数据库的flutter应用程序中的列表中的搜索操作实现以下代码。

When no search is implemented it loads the list fine, but when I try to use the search operation for the same it gives me error当没有实现搜索时,它会很好地加载列表,但是当我尝试使用相同的搜索操作时,它给了我错误

ERROR : type 'Query' is not a subtype of type 'Stream'错误:“查询”类型不是“流”类型的子类型

Scaffold(
  appBar: AppBar(
    elevation: 0,
    title:                     Container(
      padding: EdgeInsets.symmetric(horizontal: 10),

      child: Card(
        child: TextField(
          decoration: InputDecoration(
              prefixIcon: Icon(Icons.search), hintText: 'Search...'),
          onChanged: (val) {
            setState(() {
              query = val;
            });
          },
        ),
      ),

    ),

    leading: IconButton(
        icon: Icon(
          Icons.exit_to_app,
          color: Colors.red[50],
        ),
        onPressed: () {
          logout();
        }),
  ),
  body:

  Container(
          child: Column(
            children: [
             


              Container(
                child: Expanded(
                  child: StreamBuilder<QuerySnapshot>(
                    stream: (query != "" && query != null)
                        ? FirebaseFirestore.instance
              .collection('momos_nv')
      .orderBy('itemName')
      .startAt([query])
      .endAt([query + '\uf8ff'])
                        : FirebaseFirestore.instance.collection("momos_nv").snapshots(),
                    builder: (context, snapshot) {

                      if (snapshot.hasError) {
                        return Text('Something went wrong');
                      }

                      if (snapshot.connectionState == ConnectionState.waiting) {
                        return Text("Loading");
                      }

                      return ListView.builder(
                          itemCount: snapshot.data.docs.length,
                          itemBuilder: (context, index){

                   
                            return ListTile(title:Text(snapshot.data.docs[index]['itemName'],
                            ),
                            );
                          });

                    },
                  ),
                ),
              ),

            ],
          ),
        ));

I am not able to resolve it , as am not able to understand where the wrong is happening when the search is done, I have referred any example from GitHub which working fine but when replicating the same it is not working well.我无法解决它,因为在搜索完成时我无法理解哪里发生了错误,我已经从 GitHub 中引用了任何工作正常的示例,但是在复制它时却无法正常工作。 Please advise the right implementation for the same.请告知正确的实现方法。

The error comes I just started typing in the search box.错误来了,我刚开始在搜索框中输入。

The example I have referred is this one from Github referred sample code我提到的例子是 Github提到的示例代码

Container(
                child: Expanded(
                  child: StreamBuilder<QuerySnapshot>(
                    stream: (query != "" && query != null)
                        ? FirebaseFirestore.instance
              .collection('momos_nv')
      .orderBy('itemName')
      .startAt([query])
      .endAt([query + '\uf8ff']).snapshots() ----> you have missed the snapShot here
                        : FirebaseFirestore.instance.collection("momos_nv").snapshots(),
  1. You have missed the snapShot on stream if condition如果条件允许,您错过了直播中的 snapShot

  2. Please look the below steam condition to resolve it.请查看下面的蒸汽条件来解决它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM