[英]How to make firestore search results display in flutter?
I am trying to display the IMEI numbers related to the search of the user, but I don't know where I'm doing wrong the results are not appearing.我正在尝试显示与用户搜索相关的 IMEI 号码,但我不知道我在哪里做错了结果没有出现。 I'm new to flutter.
我是 flutter 的新手。 This is my code.
这是我的代码。 please help
请帮忙
class _HomeState extends State<Home> {
TextEditingController seachtf = TextEditingController();
@override
Widget build(BuildContext context) {
final Stream<QuerySnapshot> _usersStream = FirebaseFirestore.instance
.collection('imei')
.where(
'number',
isEqualTo: seachtf.text,
)
.snapshots();
return Scaffold(
appBar: AppBar(
title: Container(
padding: EdgeInsets.only(
left: 20,
right: 10,
),
child: TextField(
controller: seachtf,
decoration: InputDecoration(
hintText: 'Search',
),
onChanged: (value) {
setState(() {});
},
),
),
),
body: StreamBuilder(
stream: _usersStream,
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) {
return Text("something is wrong");
}
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(
child: CircularProgressIndicator(),
);
}
return Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(12),
),
child: ListView.builder(
itemCount: snapshot.data!.docs.length,
itemBuilder: (_, index) {
var data = snapshot.data!.docs[index].data()
as Map<String, dynamic>;
return Card(
child: ListTile(
title: Text(
data['number'].toString(),
),
),
);
},
),
);
},
),
);
}
}
First of all the way you are implementing the "search method" is way complicated and inefficient.首先,您实现“搜索方法”的方式既复杂又低效。
Flutter has pre-built implementation called ' SearchDelegate '. Flutter 具有名为“ SearchDelegate ”的预构建实现。 If you check it's documentation and search for it's use examples with Firebase Firestore database it will make your life much easier.
如果您检查它的文档并使用 Firebase Firestore 数据库搜索它的使用示例,它将使您的生活更加轻松。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.