Which method creates a new user with email and password?
createUserWithEmailAndPassword() creates a new Firebase user account with the given email and password.
Add email/password and Google sign-in to your app
You can read this lesson publicly. Sign in to unlock exercises, quiz, and FX progress tracking.
Having trouble? AI is here to help
Ask context-aware questions about this lesson. Free plan: 5 questions/day. Upgrade to Premium for higher daily limits.
Use the AI button at the bottom of the screen
Firebase Authentication handles the entire user identity flow for your app including sign-up, sign-in, password reset, and session management. It supports multiple sign-in methods such as email and password, Google Sign-In, Apple Sign-In, phone number verification, and anonymous authentication. Firebase Auth also manages user sessions automatically so your app stays logged in across restarts. You can listen to authentication state changes with a stream and reactively update your UI whenever the user signs in or signs out.
1// pubspec.yaml: firebase_auth: ^4.15.02import 'package:firebase_auth/firebase_auth.dart';34final auth = FirebaseAuth.instance;56// ── Sign Up ──7Future<User?> signUp(String email, String password) async {8try {9final credential = await auth.createUserWithEmailAndPassword(10email: email,11password: password,12);13return credential.user;14} on FirebaseAuthException catch (e) {15if (e.code == 'weak-password') print('Password too weak');16if (e.code == 'email-already-in-use') print('Email taken');17return null;18}19}2021// ── Sign In ──22Future<User?> signIn(String email, String password) async {23try {24final credential = await auth.signInWithEmailAndPassword(25email: email,26password: password,27);28return credential.user;29} on FirebaseAuthException catch (e) {30if (e.code == 'user-not-found') print('No user found');31if (e.code == 'wrong-password') print('Wrong password');32return null;33}34}3536// ── Sign Out ──37Future<void> signOut() async => await auth.signOut();3839// ── Auth State Listener ──40auth.authStateChanges().listen((User? user) {41if (user == null) {42print('User is signed out');43} else {44print('User signed in: ${user.email}');45}46});4748// ── Current User ──49final user = auth.currentUser;50print(user?.email);51print(user?.uid);
Use the authStateChanges() stream to reactively update your UI when the user signs in or out. Wrap your root widget with a StreamBuilder listening to this stream so the app automatically shows the login screen or home screen based on auth status.
Implement auth as a full lifecycle, not only sign in and sign out screens:
This architecture keeps UI simple and gives one place to manage security and error behavior.
Add email/password and Google sign in to your app. Backend integration connects app actions to real user data and secure business workflows.
Think in architecture, reliability, and team-level maintainability.
Define data model and access rules before writing convenience helper methods.
Security and data integrity issues can damage user trust faster than UI bugs.
Security rules are part of architecture, not optional deployment polish.
As your app grows, keep UI, state, and data responsibilities separate so each part stays testable and easier to modify.
createUserWithEmailAndPassword() creates a new Firebase user account with the given email and password.
authStateChanges() returns a Stream<User?> that emits whenever the auth state changes (sign in/out).
currentUser is null when no user is signed in. Always null-check before using it.
Something seems incorrect, unclear, or missing? Let us know.
FlutterCraft editorial lessons are reviewed by experienced Flutter developers and updated to stay aligned with modern Flutter and Dart best practices.
Reference docs: Flutter Documentation, Dart Language Tour, and official package documentation where relevant.
Sign in to continue with exercises and quiz for this lesson.
Sign In to Continue