Hello, this is Dr. I.P. Ho. Today, we will learn how to create a login button using Google API in flutter.



The related code is as below.

import 'package:google_sign_in/google_sign_in.dart';
import 'package:firebase_auth/firebase_auth.dart';

final GoogleSignIn _googleSigIn= GoogleSignIn(); // Create an object inside a class

fianl FirebaseAuth _auth=FirebaseAuth.instanse; // Create an instance inside a class



// You need to load two packages.

SignInbutton(

Button.Google,

onPressed:(){

_handleSignIn()  // below method

}

)



//method

Future<UserCredential> _handleSignIn() async{

GoogleSignInAccount googleUser = await _googleSignIn.signIn();

GoogleSignInAuthentication googleAuth= await googleUser.authentication;


Final GoogleAuthCredential credential = GoogleAuthProvider.credential(

accessToken: googleAuth.accessToken,

idToken: googleAuth.idToken,

);

Return await FirebaseAuth.instance.signInWithCredential(credential);

}