简体   繁体   中英

How to search a collection in firestore through flutter?

I am trying to allow a user to search a collection. The user has to enter a unique ID to get the data which will be provided.

TextField(
                  controller: controller,
                  onChanged: (value) {
                    string = value;
                  },
                  decoration:
                      decoration.copyWith(labelText: 'search'),
                ),
StreamBuilder(
                    stream: FirebaseFirestore.instance
                        .collection('Collection Path')
                        .where('Field', isEqualTo: string)
                        .snapshots(),
                    builder: (context, snapshot) {
                      if (!snapshot.hasData) {
                        return Text("No Data found");
                      }
                      var user= snapshot.data;
                      return Text(
                                "${user["Name"]}.",
                                
                            ),
                      );
                    }),

It does not show anything. How can I fix my code? Does the code make sense?

Make sure your outer widget is a StatefulWidget . Also change

onChanged: (value) {
                    string = value;
                  },

to

onChanged: (value) {
   setState(() {
             string = value;
       });
},

to update the value of your string variable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM