2

I am working on a flutter app that has registration screen. I have implemented the email auth and want to check if the user already registered or not? I have used:

final newUser = await _auth.createUserWithEmailAndPassword(email: email.trim(),password: password);

to register the user. if the user has already registered, it throw an error that days the email is already in use, but what i want is to customize the message. so, how to retrieve the a specific user data (e.g email..) from firebase collection by giving an specific email? for example: before registration call, want to query from collection if the specified email is already in use or not?

i have tried the fetchSignInMethodsForEmail as well, but could not get any result.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Hekmat
  • 588
  • 6
  • 19
  • EDIT My bad that was android here is another link: https://stackoverflow.com/questions/51122211/check-if-field-already-exists-in-flutter-firestore – HenryMigo Apr 03 '20 at 11:34
  • "i have tried the `fetchSignInMethodsForEmail` as well, but could not get any result." That is the correct method to use, so please edit your question to show what you tried with it so we can check why it didn't work for you. – Frank van Puffelen Apr 03 '20 at 14:49

1 Answers1

1

Solved!

Future<void> isEmailRegistered(String email) async {
final QuerySnapshot result = await Firestore.instance
    .collection('users')
    .where('userEmail', isEqualTo: email)
    .limit(1)
    .getDocuments();
final List<DocumentSnapshot> documents = result.documents;
if (documents.length > 0)
  print('Email is in Use');
else
  print('Email is not in Use');

}

Hekmat
  • 588
  • 6
  • 19