【发布时间】:2020-02-07 00:27:08
【问题描述】:
我想将我的搜索键存储在一个数组中,这样我就可以用一行代码搜索多个内容,但它不起作用。
我正在使用基本搜索,这正是https://github.com/rajayogan/flutterfirestore-instantsearch
我所拥有的基本上是多个具有数组的文档,一个数组由包含 item_name 等信息的地图组成……我要搜索的是 item_name。
我试着放了
import 'package:cloud_firestore/cloud_firestore.dart';
//This class should take the array search which is that I wish to be my searchkeys
class SearchService {
searchByName(String searchField) {
return Firestore.instance
.collection('Foodtruckinfo')
.where('arraysearch',
isEqualTo: searchField.substring(0, 1).toUpperCase())
.getDocuments();
}
}
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:customer_register_login/Customer/searchmethod.dart';
import 'package:flutter/material.dart';
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
var queryResultSet = [];
var tempSearchStore = [];
initiateSearch(value) {
if (value.length == 0) {
setState(() {
queryResultSet = [];
tempSearchStore = [];
});
}
var capitalizedValue =
value.substring(0, 1).toUpperCase() + value.substring(1);
if (queryResultSet.length == 0 && value.length == 1) {
SearchService().searchByName(value).then((QuerySnapshot docs) {
for (int i = 0; i < docs.documents.length; ++i) {
queryResultSet.add(docs.documents[i].data);
}
});
} else {
tempSearchStore = [];
queryResultSet.forEach((element) {
if (element['item_name'].startsWith(capitalizedValue)) {
setState(() {
tempSearchStore.add(element);
});
}
});
}
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: Text('Firestore search'),
),
body: ListView(children: <Widget>[
Padding(
padding: const EdgeInsets.all(10.0),
child: TextField(
onChanged: (val) {
initiateSearch(val);
},
decoration: InputDecoration(
prefixIcon: IconButton(
color: Colors.black,
icon: Icon(Icons.arrow_back),
iconSize: 20.0,
onPressed: () {
Navigator.of(context).pop();
},
),
contentPadding: EdgeInsets.only(left: 25.0),
hintText: 'Search by name',
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(4.0))),
),
),
SizedBox(height: 10.0),
GridView.count(
padding: EdgeInsets.only(left: 10.0, right: 10.0),
crossAxisCount: 2,
crossAxisSpacing: 4.0,
mainAxisSpacing: 4.0,
primary: false,
shrinkWrap: true,
children: tempSearchStore.map((element) {
return buildResultCard(element);
}).toList())
]));
}
}
Widget buildResultCard(data) {
return Card(
shape: RoundedRectangleBorder(borderRadius:
BorderRadius.circular(10.0)),
elevation: 2.0,
child: Container(
child: Center(
child: Text(
data['item_name'],
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.black,
fontSize: 20.0,
),
))));
}
【问题讨论】:
标签: firebase flutter dart google-cloud-platform google-cloud-firestore