Home > other >  Is there a way to check the value of a specific field in a document?
Is there a way to check the value of a specific field in a document?

Time:07-11

I'm trying to compare the text inputted by user to my firestore collection.

But when I tried this code:

docSnapshot.data()!.containsValue(currentStudPass);

where currentStudPass is a String that contains the User ID which is 184-0055

It works but it's trying to search for all the fields value that the user has, for example I tried "Test Section" as a password, it will return true.

I want to exclude all fields except the password: or just use containsValue for password: field. But I don't have any idea how to do that I'm new to Flutter.

I tried:

docSnapshot.data()!.containsValue('password: $currentStudPass');

var docSnapshot = await collection.doc("$currentStudId/password").get();

But it doesn't work.

Here's my firestore db.enter image description here

static Future<bool> checkPass(String passArg) async {
    // method to check if the users password is correct
    var collection = FirebaseFirestore.instance.collection('users');
    var docSnapshot = await collection.doc("$currentStudId").get();

    try {
      passCheck =
          docSnapshot.data()!.containsValue(currentStudPass);
      return passCheck;
    } catch (e) {
      // If any error
      return false;
    }

CodePudding user response:

try this if this works.

  static Future<bool> checkPass({String? id, String? passArg}) async {
     
     final collection = FirebaseFirestore.instance.collection('users');
     final docs = await collection.doc(id!).get();
     final Map<String,dynamic> map = docs.data() as Map<String,dynamic>;
      
      // you can direct it since the result from contains is bool
     return map['password'].toString().contains(passArg!);
    
    }
  • Related