简体   繁体   English

如何使 Firestore 搜索结果显示在 flutter 中?

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

相关问题 如何使用 flutter 在 firestore 的卡片列表中显示我的结果 - How to display my results in a list of cards from firestore with flutter 如何将图像存储在 flutter 火库中并显示它 - How to store image in flutter firestore and display it Flutter Firestore:如何从Firestore中获取map数据并显示在Flutter Widget中 - Flutter Firestore: How to retrieve map data from Firestore and display in Flutter Widget 如何从 Firestore 数据库中检索字符串数组并显示它 - flutter - how to retrieve an array of strings from Firestore database and display it - flutter 如何在 Flutter 的 SfCalendar 数据源中显示来自 Firestore 的约会? - How to display appointment from Firestore in SfCalendar's data source in Flutter? 如何使用 flutter 和 firebase 在 Firestore 中使用 URL 作为字符串在 App 中显示图像 - How to display Image in App with flutter and firebase using URL as String in Firestore flutter 在 firestore 中搜索两个项目 - flutter search in firestore for two items 如何在 flutter 中实现刷卡并显示来自 Firestore 的交易 - How to implement the card swipe and display its transaction from firestore in flutter 如何在 flutter stream 生成器中使用不同的 firestore 集合创建 where 条件? - How to make where condition with different firestore collection in flutter stream builder? flutter中Firestore数据变化结果为null - Firestore data change results is null in flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM