0

currenty when somebody registers in my app, im providing an Input field which returns if the entered username already exists in the database.

Future getUserName(String userNameFieldInput) {
    print('userNameFieldInput: $userNameFieldInput');
    return FirebaseFirestore.instance
        .collection('usernames')
        .doc(userNameFieldInput)
        .get()
        .then((value) => value.exists ? true : false)
        .catchError((error) => print(error));
  }

Im returning true or false and thats totally fine. Im not sure if my database structure is fine for that. I would love to have some recommendations on here. Also how many reads do we have here? IM using the Firestore.

MyDatabase
 |
 ---Users
 |   |
 |    ---THE_USERS_UID
 |         |
 |         |--name: "marcel"
 |         |
 |         |---gender: "male"
 |         |...
 |
 ---usernames
     |
     ---marcel
          |
          |---uid: "THE_USERS_UID"

1 Answer 1

1

Yup, this looks fine to me.

If you want something to be unique, you should use that as the ID of your documents in a collection, which you're doing in your usernames collection.

That also ensures that you only need to read/check for the existence of a single document to determine if a username is already in use, which is the minimum number of document reads that is possible for such an operation.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.