[英]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.