简体   繁体   English

Flutter Firebase 苹果登录不工作 ios

[英]Flutter Firebase Sign In With Apple Not Working ios

I am trying to set up with apple sign in with the following code.我正在尝试使用以下代码设置苹果登录。 When tapping the button nothing happens.点击按钮时没有任何反应。

I get the following error:我收到以下错误:

NoSuchMethodError (NoSuchMethodError: The method '[]' was called on null.
Receiver: null
Tried calling: []("User"))

The new user appears in firebase authentication but other than that the app does not work.新用户出现在 firebase 身份验证中,但除此之外该应用程序不起作用。

I have enabled sign in with apple in xcode and on developer.apple.com.我已经在 xcode 和 developer.apple.com 中启用了使用苹果登录。 I have also tried redownloading my provisioning profiles in xcode but no luck.我也曾尝试在 xcode 中重新下载我的配置文件,但没有成功。

I have enabled apple sign in in firebase.我在 firebase 中启用了苹果登录。

Does anyone know how to fix this?有谁知道如何解决这一问题?

mixin AuthenticationApple {
  static String generateNonce([int length = 32]) {
    const charset =
        '0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz-._';
    final random = Random.secure();
    return List.generate(length, (_) => charset[random.nextInt(charset.length)])
        .join();
  }

  /// Returns the sha256 hash of [input] in hex notation.
  static String sha256ofString(String input) {
    final bytes = utf8.encode(input);
    final digest = sha256.convert(bytes);
    return digest.toString();
  }

  static Future<User> signInWithApple({BuildContext context}) async {
    User user;
    // To prevent replay attacks with the credential returned from Apple, we
    // include a nonce in the credential request. When signing in with
    // Firebase, the nonce in the id token returned by Apple, is expected to
    // match the sha256 hash of `rawNonce`.
    final rawNonce = generateNonce();
    final nonce = sha256ofString(rawNonce);

    // Request credential for the currently signed in Apple account.
    final appleCredential = await SignInWithApple.getAppleIDCredential(
      scopes: [
        AppleIDAuthorizationScopes.email,
        AppleIDAuthorizationScopes.fullName,
      ],
      nonce: nonce,
    );

    // Create an `OAuthCredential` from the credential returned by Apple.
    final oauthCredential = OAuthProvider('apple.com').credential(
      idToken: appleCredential.identityToken,
      rawNonce: rawNonce,
    );
    try {
      // Sign in the user with Firebase. If the nonce we generated earlier does
      // not match the nonce in `appleCredential.identityToken`, sign in will fail.
      final UserCredential userCredential =
          await FirebaseAuth.instance.signInWithCredential(oauthCredential);
      user = userCredential.user;
    } on FirebaseAuthException catch (e) {
      if (e.code == 'account-exists-with-different-credential') {
        ScaffoldMessenger.of(context).showSnackBar(
          AppWidget.customSnackBar(
            content: 'The account already exists with a different credential.',
          ),
        );
      } else if (e.code == 'invalid-credential') {
        ScaffoldMessenger.of(context).showSnackBar(
          AppWidget.customSnackBar(
            content: 'Error occurred while accessing credentials. Try again.',
          ),
        );
      }
    } catch (e) {
      ScaffoldMessenger.of(context).showSnackBar(
        AppWidget.customSnackBar(
          content: 'Error occurred using Apple Sign-In. Try again.',
        ),
      );
    }
    return user;
  }
}

I just surpassed this issue by using iOS 13.0 for simulator.我刚刚通过使用 iOS 13.0 作为模拟器解决了这个问题。 It seems there's an issue with higher versions when using the simulator but not with physical devices.使用模拟器时似乎更高版本存在问题,但物理设备则没有。

You can download and install simulators all the way back to iOS 13.0 (which is the minimum version for Apple Sign-In. In Xcode:你可以下载并安装模拟器一路回到iOS 13.0(这是Apple登录的最低版本。在Xcode中:

Xcode > Preferences. Xcode > 首选项。 Select the "Components" tab. Select “组件”选项卡。 Mark the simulators you want.标记您想要的模拟器。 Press "Check and Install Now".按“立即检查并安装”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM