Email and password
- Flutter course
- Advanced
- Lesson 17/69
- 4 min
Sign up with createUserWithEmailAndPassword.
createUserWithEmailAndPassword
The createUserWithEmailAndPassword method creates a new user account with an email and password. The email and password parameters are required. On success it returns a UserCredential that contains the created user's information.
final cred = await auth.createUserWithEmailAndPassword(
email: 'a@b.com',
password: '123456',
);
print(cred.user.email);FirebaseAuthException
If createUserWithEmailAndPassword fails, a FirebaseAuthException is thrown. The error code is given in the code field; for example 'email-already-in-use' or 'weak-password'. Therefore you should wrap the call in try/catch.
try {
await auth.createUserWithEmailAndPassword(
email: email, password: password);
} on FirebaseAuthException catch (e) {
print(e.code);
}Practice Register a user
Get the auth instance, create a new user with createUserWithEmailAndPassword using email 'a@b.com' and password '123456'. Print the email from the returned UserCredential.
Starter code
class User {
final String email;
User(this.email);
}
class UserCredential {
final User user;
UserCredential(this.user);
}
class FirebaseAuth {
static final FirebaseAuth instance = FirebaseAuth._();
FirebaseAuth._();
User? _user;
User? get currentUser => _user;
Future<UserCredential> createUserWithEmailAndPassword({
required String email,
required String password,
}) async {
_user = User(email);
return UserCredential(_user!);
}
}
void main() async {
// kayıt metodunu çağır
// sonuçtan e-postayı yazdır
}Sample solution
class User {
final String email;
User(this.email);
}
class UserCredential {
final User user;
UserCredential(this.user);
}
class FirebaseAuth {
static final FirebaseAuth instance = FirebaseAuth._();
FirebaseAuth._();
User? _user;
User? get currentUser => _user;
Future<UserCredential> createUserWithEmailAndPassword({
required String email,
required String password,
}) async {
_user = User(email);
return UserCredential(_user!);
}
}
void main() async {
final auth = FirebaseAuth.instance;
final cred = await auth.createUserWithEmailAndPassword(
email: 'a@b.com',
password: '123456',
);
print(cred.user.email);
}Expected output
a@b.com
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What does createUserWithEmailAndPassword return on success?
- Only an error code
- A FirebaseAuth instance
- UserCredential Correct answer
- An empty Future
Explanation: On successful registration a UserCredential is returned; its user field gives access to the created user.
2 Call the registration method
final cred = await auth.____(
email: 'a@b.com',
password: '123456',
);
final cred = await auth.createUserWithEmailAndPassword(
email: 'a@b.com',
password: '123456',
);
Explanation: To create a new user you call createUserWithEmailAndPassword; the email and password parameters are required.
3 Registration parts
| Term | Meaning |
|---|---|
createUserWithEmailAndPassword |
Creates a new user |
UserCredential |
Operation result |
email: |
Email address |
password: |
Password |