Google Sign-in with a different account is not working (FlutterFire Package)

Viewed 373

When I Sign-in with one of my Google accounts the sign-in works perfectly. but after signing out, when I try to sign in with a different account, instead of popping up the google accounts list the app directly sign-ins to the previous account I signed in to.

This is the Code Snippet for Main.dart main.dart ->>

import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:social_media_app/widget/auth_gate.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: const Size(375, 667),
      minTextAdapt: true,
      builder: () {
        return GetMaterialApp(
          debugShowCheckedModeBanner: false,
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: const AuthGate(),
        );
      },
    );
  }
}

This is the code snippet of the AuthGate. auth_gate.dart -->

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:flutterfire_ui/auth.dart';

class AuthGate extends StatelessWidget {
  const AuthGate({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return StreamBuilder<User?>(
      stream: FirebaseAuth.instance.authStateChanges(),
      builder: (context, snapshot) {
        if (!snapshot.hasData) {
          return SignInScreen(
            // showAuthActionSwitch: false,
            subtitleBuilder: (context, action) {
              return Padding(
                padding: const EdgeInsets.only(bottom: 8),
                child: Text(
                  action == AuthAction.signIn
                      ? 'Welcome to FlutterFire UI! Please sign in to continue.'
                      : 'Welcome to FlutterFire UI! Please create an account to continue',
                ),
              );
            },
            footerBuilder: (context, _) {
              return const Padding(
                padding: EdgeInsets.only(top: 16),
                child: Text(
                  'By signing in, you agree to our terms and conditions.',
                  style: TextStyle(color: Colors.grey),
                ),
              );
            },
            sideBuilder: (context, constraints) {
              return Padding(
                padding: const EdgeInsets.all(20),
                child: AspectRatio(
                  aspectRatio: 1,
                  child: Image.network(
                      'https://firebase.flutter.dev/img/flutterfire_300x.png'),
                ),
              );
            },
            headerBuilder: (context, constraints, _) {
              return Padding(
                padding: const EdgeInsets.all(20),
                child: AspectRatio(
                  aspectRatio: 1,
                  child: Image.network(
                    'https://firebase.flutter.dev/img/flutterfire_300x.png',
                  ),
                ),
              );
            },
            providerConfigs: const [
              EmailProviderConfiguration(),
              PhoneProviderConfiguration(),
              GoogleProviderConfiguration(
                clientId: '1:778935804680:android:d2da33128b67cdc68c42bd',
              ),
              FacebookProviderConfiguration(
                clientId: '...',
              ),
            ],
          );
        }
        return const ProfileScreen(
          providerConfigs: [
            EmailProviderConfiguration(),
            GoogleProviderConfiguration(
              clientId: '1:778935804680:android:d2da33128b67cdc68c42bd',
            ),
            FacebookProviderConfiguration(
              clientId: '...',
            ),
          ],
          avatarSize: 200,
        );
      },
    );
  }
}
2 Answers

I take it you did first sign-out of google in any open browsers right? I believe the idea is to speed up login by using often already established Google logins. So if you have more, log off in other windows.

You should add an extra line of code where you explicitly diconnect from googleSignIn.

final googleSignIn = GoogleSignIn();

. . .

Future logout() async {
    await googleSignIn.disconnect();
    FirebaseAuth.instance.signOut();
}

Also, be sure to add the google_sign_in package :)

Related