Flutter ile Google ile kayıt olma (Google sign-in) işlemi için Firebase Authentication ve Google Sign-In paketlerini kullanabilirsiniz.
a. pubspec.yaml Dosyasına Paket Ekleme
google_sign_in: ^5.3.0
Main dosyasına
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
bu kodu ekliyoruz daha sonra alttaki kodu sayfamıza ekliyoruz
class _GoogleSignInPageState extends State<GoogleSignInPage> {
final FirebaseAuth _auth = FirebaseAuth.instance;
final GoogleSignIn _googleSignIn = GoogleSignIn();
Future<User?> _signInWithGoogle() async {
final GoogleSignInAccount? googleUser = await _googleSignIn.signIn();
if (googleUser == null) {
return null; // Kullanıcı giriş yapmayı iptal etti
}
final GoogleSignInAuthentication googleAuth = await googleUser.authentication;
final OAuthCredential credential = GoogleAuthProvider.credential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
UserCredential userCredential = await _auth.signInWithCredential(credential);
return userCredential.user;
}
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Google Sign-In')),
body: Center(
child: ElevatedButton(
onPressed: () async {
User? user = await _signInWithGoogle();
if (user != null) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text('Hoş geldiniz, ${user.displayName}'),
));
}
},
child: Text('Google ile Giriş Yap'),
),
),
);
}
}
ElevatedButton ekledikten sonra bir ile _signInWithGoogle tetikliyoruz
Bu Yazıya Tepkin Ne Oldu ?
YORUMLAR