flutter google_sign_in 仅在第一次打开登录窗口(弹出窗口)

问题描述 投票:0回答:1

我正在使用官方

google_sign_in
包让用户登录到我的 Flutter 应用程序。当用户第一次想要登录时,会打开一个弹出窗口,从可用帐户列表中进行选择,但是当用户注销并想要再次登录时,它会自动让用户登录到之前的帐户,从而不打开登录弹出窗口。

我希望每次都打开弹出窗口,以便用户可以选择不同的 Google 帐户。

这是我使用的代码:

Future<UserCredential> signInWithGoogle() async {
    // Trigger the authentication flow
    final GoogleSignInAccount? googleUser = await GoogleSignIn().signIn();

    // Obtain the auth details from the request
    final GoogleSignInAuthentication? googleAuth =
        await googleUser?.authentication;

    // Create a new credential
    final credential = GoogleAuthProvider.credential(
      accessToken: googleAuth?.accessToken,
      idToken: googleAuth?.idToken,
    );

    // Once signed in, return the UserCredential
    return await auth.signInWithCredential(credential);
  }
flutter firebase firebase-authentication google-signin
1个回答
0
投票

解决办法很简单。您只需使用

signOut
功能即可。

这是代码:

    Future<UserCredential> signInWithGoogle() async {
        // Trigger the authentication flow
        final GoogleSignInAccount? googleUser = await GoogleSignIn().signIn();
    
        // Obtain the auth details from the request
        final GoogleSignInAuthentication? googleAuth =
            await googleUser?.authentication;
    
        // Create a new credential
        final credential = GoogleAuthProvider.credential(
          accessToken: googleAuth?.accessToken,
          idToken: googleAuth?.idToken,
        );

        // Once signed in, return the UserCredential
      final UserCredential userCredential =
          await auth.signInWithCredential(credential);

      final GoogleSignInAccount? user = await GoogleSignIn().signOut(); // <-- add this code here

      return userCredential;
  }

当您通过

GoogleSignIn().signIn()
获取必要的用户信息并执行某些操作后,您可以应用
GoogleSignIn().signOut()
来删除现有的用户信息缓存。

或者,您可以通过调用并定义注销函数来完成。

  Future<void> signOut() async {
    try {
      await GoogleSignIn().signOut();
    } catch (e) {
      debugPrint('Error signing out. Try again.');
    }
  }
© www.soinside.com 2019 - 2024. All rights reserved.