Flutter Firebase Sign In with Different Methods

Required Packages

provider: ^4.3.1

cloud_firestore: ^0.13.7

firebase_auth:

google_sign_in:

Create instance of firebase auth:

final FirebaseAuth _auth = FirebaseAuth.instance;

create user model :

class User {final String uid;User({this.uid});}user_model.dart

custom user object based on our user model:

User _userFromFirebaseUser(FirebaseUser user) {return user != null ? User(uid: user.uid) : null;}

Sign in Anonymously

Future signInAnon() async {try {AuthResult result = await _auth.signInAnonymously();FirebaseUser user = result.user;return _userFromFirebaseUser(user);} catch (e) {print(e);return null;}}

Register and Login with Email and Password:

//Register in email and passFuture signUpWithEmailPass(String email, String password) async {try {AuthResult result = await _auth.createUserWithEmailAndPassword(email: email, password: password);FirebaseUser user = result.user;return _userFromFirebaseUser(user);} catch (e) {print(e.toString());return null;}}//Sign in with email and passFuture signInWithEmailPass(String email, String password) async {try {AuthResult result = await _auth.signInWithEmailAndPassword(email: email, password: password);FirebaseUser user = result.user;return _userFromFirebaseUser(user);} catch (e) {print(e.toString());return null;}}

Sign Out:

// Sign OutFuture signOut() async {try {_auth.signOut();} catch (e) {print("Error signing out");return null;}}

Google Sign In:

Create instance of GoogleSignIn,

GoogleSignIn _googleSignIn = GoogleSignIn();

Implement Google Sign In this way

//Google Sign InFuture googleSignIn() async {try {final GoogleSignInAccount _googleSignInAccount = await _googleSignIn.signIn();final GoogleSignInAuthentication _googleSignInAuth = await _googleSignInAccount.authentication;final AuthCredential credential = GoogleAuthProvider.getCredential(idToken: _googleSignInAuth.idToken,accessToken: _googleSignInAuth.accessToken);final FirebaseUser user = (await _auth.signInWithCredential(credential)).user;return _userFromFirebaseUser(user);} catch (e) {print(e.toString());}}

call any method by creating instance of AuthService class and feel the power of firebase.