Flutter Bloc (v8): Google Sign In and Firebase Authentication - 2022 Guide
A blog around using Firebase Auth and BLoC Architecture to authenticate users with email/password and Google Sign In.

Search for a command to run...
A blog around using Firebase Auth and BLoC Architecture to authenticate users with email/password and Google Sign In.

Hi! How could I modify the code, that after signin or signout, the username would change automatically? (And how to change when I would like to display user account chooser popup?
Thank you Andres Reyes. Glad that you liked it 😀.
Lessons from Code, Content, and Consistency

Reliability basics: Outbox pattern + why it matters

Introducing Kafka/Redpanda + move to event-driven workflow

Connect Orders ↔ Inventory (first working service-to-service flow)

Inventory service + gRPC + proto contracts







Do not forget to add an updated google-services.json file in your project.
dependencies:
firebase_core: ^1.10.6
firebase_auth: ^3.3.4
equatable: ^2.0.3
flutter_bloc: ^8.0.1
google_sign_in: ^5.2.1
email_validator: ^2.0.1
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(const MyApp());
}
lib\data\repositories\auth_repository.dartclass AuthRepository{
final _firebaseAuth = FirebaseAuth.instance;
}
Future<void> signUp({required String email, required String password}) async {
try {
await FirebaseAuth.instance
.createUserWithEmailAndPassword(email: email, password: password);
} on FirebaseAuthException catch (e) {
if (e.code == 'weak-password') {
throw Exception('The password provided is too weak.');
} else if (e.code == 'email-already-in-use') {
throw Exception('The account already exists for that email.');
}
} catch (e) {
throw Exception(e.toString());
}
}
Future<void> signIn({
required String email,
required String password,
}) async {
try {
await FirebaseAuth.instance
.signInWithEmailAndPassword(email: email, password: password);
} on FirebaseAuthException catch (e) {
if (e.code == 'user-not-found') {
throw Exception('No user found for that email.');
} else if (e.code == 'wrong-password') {
throw Exception('Wrong password provided for that user.');
}
}
}
Future<void> signOut() async {
try {
await _firebaseAuth.signOut();
} catch (e) {
throw Exception(e);
}
}
Let's make a method called signInWithGoogle for Google Sign In. Which is in charge of displaying the Google Sign In Dialog and logging in with a Google account.
Future<void> signInWithGoogle() async {
try {
final GoogleSignInAccount? googleUser = await GoogleSignIn().signIn();
final GoogleSignInAuthentication? googleAuth =
await googleUser?.authentication;
final credential = GoogleAuthProvider.credential(
accessToken: googleAuth?.accessToken,
idToken: googleAuth?.idToken,
);
await FirebaseAuth.instance.signInWithCredential(credential);
} catch (e) {
throw Exception(e.toString());
}
}
auth_repository.dart code Hereauth_state.dart)auth_event.dart)lib\bloc\auth_events.dart.auth_bloc.dart)class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return RepositoryProvider(
create: (context) => AuthRepository(),
child: BlocProvider(
create: (context) => AuthBloc(
authRepository: RepositoryProvider.of<AuthRepository>(context),
),
child: MaterialApp(
home: SignIn()
),
),
);
}
}
RepositoryProvider.of<AuthRepository>(context)void _authenticateWithEmailAndPassword(context) {
if (_formKey.currentState!.validate()) {
// If email is valid adding new Event [SignInRequested].
BlocProvider.of<AuthBloc>(context).add(
SignInRequested(_emailController.text, _passwordController.text),
);
}
}
//
void _authenticateWithGoogle(context) {
BlocProvider.of<AuthBloc>(context).add(
GoogleSignInRequested(),
);
}
IconButton(
onPressed: () {
_authenticateWithGoogle(context);
},
icon: ...
),
//
SizedBox(
width: MediaQuery.of(context).size.width * 0.7,
child: ElevatedButton(
onPressed: () {
_authenticateWithEmailAndPassword(context);
},
child: const Text('Sign In'),
),
)
void _authenticateWithEmailAndPassword(context) {
if (_formKey.currentState!.validate()) {
// If email is valid adding new event [SignUpRequested].
BlocProvider.of<AuthBloc>(context).add(
SignUpRequested(_emailController.text, _passwordController.text),
);
}
}
//
void _authenticateWithGoogle(context) {
BlocProvider.of<AuthBloc>(context).add(
GoogleSignInRequested(),
);
}
IconButton(
onPressed: () {
_authenticateWithGoogle(context);
},
icon: ...
),
//
SizedBox(
width: MediaQuery.of(context).size.width * 0.7,
child: ElevatedButton(
onPressed: () {
_createAccountWithEmailAndPassword(context);
},
child: const Text('Sign Up'),
),
)
final user = FirebaseAuth.instance.currentUser!;



main.dart and paste the code below.
