Flutter firebase_auth signInWithCredential 在android上不起作用

Posted

技术标签:

【中文标题】Flutter firebase_auth signInWithCredential 在android上不起作用【英文标题】:Flutter firebase_auth signInWithCredential is not working on android 【发布时间】:2019-06-30 10:09:24 【问题描述】:

Greetings Flutter 开发人员正在开发一款适用于 iosandroid 的应用,该应用使用手机身份验证来验证用户身份,在 iOS 中一切正常,但对于 android,当调用此行时出现错误:

    final FirebaseUser user = await _auth.signInWithCredential(credential);

错误:

E/flutter (14190): [ERROR:flutter/shell/common/shell.cc(184)] Dart 错误:未处理的异常: E/颤振(14190):PlatformException(错误,空引用,空) E/flutter (14190): #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:551:7) E/flutter (14190): #1 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:292:18) E/颤振(14190): E/flutter (14190): #2 FirebaseAuth.signInWithCredential (file:///Users/pocketcarage/Desktop/flutter/.pub-cache/hosted/pub.dartlang.org/firebase_auth-0.8.0+1/lib/src /firebase_auth.dart:211:54) E/颤振(14190): E/flutter (14190): #3 _RegisterState.signIn (package:pocketcarage_official/UI/register.dart:92:43) E/颤振(14190): E/flutter (14190): #4 _RegisterState.smsCodeDialog.. (package:pocketcarage_official/UI/register.dart:77:23) E/flutter (14190): #5 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:507:14) E/颤振(14190):#6 _InkResponseState.build。 (包:flutter/src/material/ink_well.dart:562:30) E/flutter (14190): #7 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:102:24) E/flutter (14190): #8 TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:242:9) E/flutter (14190): #9 TapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:175:7) E/flutter (14190): #10 PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:315:9) E/flutter (14190): #11 PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:73:12) E/flutter (14190): #12 PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:101:11) E/flutter (14190): #13 _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:180:19) E/flutter (14190): #14 _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:158:22) E/flutter (14190): #15 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:138:7) E/flutter (14190): #16 _WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:101:7) E/flutter (14190): #17 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:85:7) E/flutter (14190): #18 _invoke1 (dart:ui/hooks.dart:168:13) E/flutter (14190): #19 _dispatchPointerDataPacket (dart:ui/hooks.dart:122:5)

其中指出 PlatformException(error, null reference, null) 尽管它在 iPhone 上 100% 有效,但存在 null 引用。我搜索了一下,发现flutter插件存在一些问题,解决方案将迁移到androidx,我尝试了,但我不断得到所需的compilesdk至少28,我已经改变了我该怎么办?

这是被调用的代码

Future<String> signIn(String smsCode) async 

final AuthCredential credential = PhoneAuthProvider.getCredential(
  verificationId: verificationId,
  smsCode: smsCode,
);

final FirebaseUser user = await _auth.signInWithCredential(credential);


final FirebaseUser currentUser = await _auth.currentUser();
DatabaseReference user_db = pDatabase.reference().child("users");
DatabaseReference phone_id = pDatabase.reference().child("phone_id");

String uid = user.uid;
usersModel new_user = new usersModel();
new_user.phone=_phoneController.text;
new_user.email=_emailController.text;
new_user.name=_nameController.text;
user_db.child(uid).child("name").set(new_user.userName);
user_db.child(uid).child("phone").set(new_user.userPhone);
user_db.child(uid).child("email").set(new_user.userEmail);
phone_id.child(new_user.userPhone).set(uid);
assert(user.uid == currentUser.uid);
Navigator.pop(context);
Navigator.of(context).pushNamed('/Home');
return 'signInWithPhoneNumber succeeded: $user';

【问题讨论】:

【参考方案1】:

更新:对我的项目进行以下更改后,登录方法似乎可以正常工作:

在 pubspec.yaml 中: 将 sdk 版本从 ">=2.0.0-dev.68.0 =2.1.0

在 android/build.gradle 中: 更新了 com.android.tools.build:gradle:3.3.1 的依赖项以使用版本 3.2.1 更新了 com.google.gms:google-services:4.0.1 的依赖项以使用版本 4.2.0

此外,由于在这些更改后最终出现了多索引的 gradle 问题,我更新了 android/app/build.gradle 文件,使其在 defaultConfig 下具有“multiDexEnabled true”属性并添加了“implementation 'com.android.support :multidex:1.0.3'" 到 buildTypes


操作:

其他人注意到了同样的问题,并在 firebase_auth Dart 包的存储库中提交了一个问题:https://github.com/flutter/flutter/issues/28057

【讨论】:

我已经完成了更改,但是当我手动输入 OTP 时仍然出现错误。【参考方案2】:

我有同样的问题。这对我有用。

(await _auth.signInWithCredential(credential)).user;

【讨论】:

【参考方案3】:

我有同样的问题,搜索一整天后我解决了问题,问题在 depandcy 请添加以下 depandancy

google_sign_in:^4.0.1+1 firebase_auth: ^0.8.1+1

【讨论】:

【参考方案4】:
try 
      final AuthCredential credential = PhoneAuthProvider.getCredential(
        verificationId: verificationId,
        smsCode: smsOTP,
      );
      final FirebaseUser user =
          (await _auth.signInWithCredential(credential)).user;
 catch (e) 
      print("Error: $e");
    

【讨论】:

以上是关于Flutter firebase_auth signInWithCredential 在android上不起作用的主要内容,如果未能解决你的问题,请参考以下文章

Flutter 中的 cloud_firestore 和 firebase_auth 兼容性问题

MissingPluginException(在通道 plugins.flutter.io/firebase_auth 上找不到方法 signInWithCredential 的实现)

Flutter - firebase_auth updateProfile 方法不起作用

Flutter 版本解决失败 firebase_auth ^1.4.1 和 http ^0.12.2

Flutter firebase_auth signInWithCredential 在android上不起作用

Flutter:StreamBuilder 快照——无数据