Home > Net >  How to prevent duplicate user information during the registration process in Flutter Firebase?
How to prevent duplicate user information during the registration process in Flutter Firebase?

Time:08-30

I'm trying to make a registration/sign-up process in my Flutter application where the users will need to enter their Voter ID along with other information in order to be registered into the system. I want the Voter ID to be a unique value (like Primary Key in RDBMS) and prevent the user if they try to register using the Voter ID that has already been taken by another registered user. How can I achieve this?

CodePudding user response:

You'll want to use the voter ID as the key/ID for identifying the user data in the database in that case.

If you're using the Realtime Database, that'd look something like:

Users: {
  "voterIdOfUser1": { ... },
  "voterIdOfUser2": { ... }
}

If you're using Cloud Firestore, you'd use the same values as the document ID with your Users collection.

CodePudding user response:

Firebase Cloud Firestore

You have to save all Unique ids while registration and before registration run a query to search that unique id is already registered or not.

FirebaseFirestore.instance
        .collection("your_collection_name")
        
        .where("unique_id",
            isEqualTo: 'user_input_value')
        .get()
        .then((value) {
      print(value.docs);
if (value.docs.isEmpty) {

// this id is already exist. show some popup

} else {

for (var element in value.docs) {
        print(element.id);
        
// Do your registration here
        
      }

}
      
    });
  • Related