'Flutter: Google sign-in method, unable to open the select email account after clicking sign in with google
I am trying to create a button to sign in using a google account in Flutter. I am using the google sign-in package for authenticating through Firebase. I have created fingerprints in firebase with SHA values and imported the google JSON file in android/app.
I am unable to get the box where the user selects the google account they want to log in with.
The error I'm getting is: "Could not find the correct Provider above this MyApp Widget"
My main file looks like this:
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider.value(
value: GoogleSignInProvider(),
child: MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
title: const Text("Check"),
),
body: Center(
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.black, onPrimary: Colors.blue),
child: const Text("Sign in with Google"),
onPressed: () {
final provider =
Provider.of<GoogleSignInProvider>(context, listen: false);
provider.googleLogin();
},
),
),
),
),
);
}
}
and my Google sign in file looks like this:
class GoogleSignInProvider extends ChangeNotifier {
final googleSignIn = GoogleSignIn();
GoogleSignInAccount? _user;
GoogleSignInAccount get user => _user!;
Future googleLogin() async {
final googleUser = await googleSignIn.signIn();
if (googleUser == null) return;
_user = googleUser;
final googleAuth = await googleUser.authentication;
final credential = GoogleAuthProvider.credential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
await FirebaseAuth.instance.signInWithCredential(credential);
notifyListeners();
}
}
Solution 1:[1]
use this when calling method sign out
googleSignin.disconnect();
You can use these 2 together as well
await authService.logout();
await googleSignin.disconnect();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | IllIIllII000 |