简体   繁体   English

搜索词未从 TextField 更新到 Firestore StreamBuilder 调用

[英]Search Term not updating from TextField to Firestore StreamBuilder Call

I am trying to implement a place where users in my app can search my Firestore Database.我正在尝试实现一个地方,让我的应用中的用户可以搜索我的 Firestore 数据库。

在此处输入图像描述

When The user enters a search term into the textField:当用户在文本字段中输入搜索词时:

return Scaffold(
      appBar: AppBar(
        title: TextField(
          textInputAction: TextInputAction.search,
          onSubmitted: (value) {
            setState(() {
              searchKey = value;
              streamQuery = db
                  .collection('GearLockerItems')
                  .where('searchTerm', isGreaterThanOrEqualTo: searchKey)
                  .where('searchTerm', isLessThan: '${searchKey}z')
                  .snapshots();
            });
          },
          decoration: const InputDecoration(
            hintText: 'Search for Gear',
            prefixIcon: Icon(Icons.search),
          ),
        ),
      )

I take the value for the search term and place it into the streamQuery我取搜索词的值并将其放入streamQuery

I then take that streamQuery and put it into the StreamBuilder:然后我将那个 streamQuery 放入 StreamBuilder 中:

body: StreamBuilder<dynamic>(
        stream: streamQuery,
        builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
          //widget build for complated call
          print('Inside stream $searchKey');

          if (snapshot.hasData) {
            return ListView.builder(

AS you can see I put a print statement in there for testing and Inside Stream $searchKey keeps showing null, which is was I default it to.如您所见,我在其中放置了一个打印语句进行测试,并且在 Stream $searchKey内部一直显示 null,这是我的默认设置。 I do not understand why when I enter data into the search box its not updating the searchKey to what ever I type and is keeping it null...我不明白为什么当我在搜索框中输入数据时,它没有将searchKey更新为我输入的内容,而是将其保留为 null ...

First fetch the data from firestore and use then function to set state.May it works and don't forget to use await because you wait untill the data is fetched.首先从 firestore 获取数据并使用 function 设置 state。它可以工作并且不要忘记使用 await 因为你等到数据被获取。

return Scaffold(
  appBar: AppBar(
    title: TextField(
      textInputAction: TextInputAction.search,
      onSubmitted: (value) {
          searchKey = value;
          await db
              .collection('GearLockerItems')
              .where('searchTerm', isGreaterThanOrEqualTo: searchKey)
              .where('searchTerm', isLessThan: '${searchKey}z')
              .snapshots().then((data) { setstate((){streamQuery = data; });});
      },
      decoration: const InputDecoration(
        hintText: 'Search for Gear',
        prefixIcon: Icon(Icons.search),
      ),
    ),
  )

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

相关问题 使用 Flutter 和 Firestore 中的新数据更新 StreamBuilder - Updating a StreamBuilder with new data in Flutter and Firestore 使用 StreamBuilder 从 Firestore 文档中获取数据 - Fetch data from Firestore Document using StreamBuilder StreamBuilder 使用 Firestore 快照中的 StreamController - StreamBuilder using StreamController from Firestore snapshots Flutter:更新单个文档时,StreamBuilder 获取其他 Firestore 文档 - Flutter: StreamBuilder gets other firestore documents when updating a single document 使用 Firestore 为 StreamBuilder 创建 Stream - Creating Stream for StreamBuilder with Firestore 如何使用futurebuilder/streambuilder(flutter)从firestore获取数组/地图并使用lisview显示 - How to get array/map from firestore and display with lisview using futurebuilder/streambuilder(flutter) Flutter Firebase 查询 Firestore 和 Streambuilder() 的问题 - Flutter Firebase Problems with Query Firestore and Streambuilder() Flutter 云Firestore StreamBuilder<documentsnapshot> 错误</documentsnapshot> - Flutter cloud firestore StreamBuilder<DocumentSnapshot> error 使用来自 Firestore 实时侦听器的数据更新图表 - Updating chart with data from Firestore realtime listener 从 Firestore 获取值时 useState 不更新 - useState not updating when getting values from Firestore
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM