Home > OS >  Is there a way in using firestore that will print a message using flutter if an array contains a str
Is there a way in using firestore that will print a message using flutter if an array contains a str

Time:11-24

Is there a way in using firestore that will print a message using flutter if an array contains a string.

I want to check if the array inside a specific document has the user email inside it. If the array contains the user email, it will show a message.

here's my current code but it is not working.

(FirebaseFirestore.instance.collection('users').where(
                                'requests',
                                arrayContains: user.email)  == true)
                        ? Text("true")
                        : Text("false");

CodePudding user response:

get the list of user emails from firestore and store in a local list [listName]. Then call listName.contains(userEmail) and store the result in a bool variable. Like this,

  bool isEmailexist =  usersList.contains(userEmail);

Finally you can display your message by checking bool value.

CodePudding user response:

The code you have merely declares a query. It doesn't yet execute that query, so there's no way yet it can check whether there are any results.

To execute the query, you will need to either call get() (as shown in the documentation on getting data once) or snapshots() (as shown in the documentation on listening for updates).

Both operations are asynchronous, so you will have to handle that in your widgets. For get(), which returns a Future, you can use a FutureBuilder, while for snapshots, which returns a Stream, you can use a StreamBuilder.

  • Related