Flutter网站中的Firebase电话身份验证

时间:2020-10-01 14:24:47

标签: flutter firebase-authentication flutter-web

我正在尝试在Flutter Web应用程序中使用(Firebase电话身份验证)手机号码登录,但这不再起作用。

我的 pubspec.yaml 代码:

 # firebase_phoneauth: ^0.0.1
  firebase_core: ^0.4.0+9
  firebase_auth: ^0.14.0+5

实际的登录代码为:

Future<bool> loginUser(String phone, BuildContext context) async {
    FirebaseAuth _auth = FirebaseAuth.instance;

    _auth.verifyPhoneNumber(
        phoneNumber: phone,
        timeout: Duration(seconds: 60),
        verificationCompleted: (AuthCredential credential) async {
          Navigator.of(context).pop();

          AuthResult result = await _auth.signInWithCredential(credential);

          FirebaseUser user = result.user;

          if (user != null) {
            Navigator.push(
                context,
                MaterialPageRoute(
                    builder: (context) => HomeScreen(
                          user: user,
                        )));
          } else {
            print("Error");
          }

          //This callback would gets called when verification is done auto maticlly
        },
        verificationFailed: (AuthException exception) {
          print(exception);
        },
        codeSent: (String verificationId, [int forceResendingToken]) {
          showDialog(
              context: context,
              barrierDismissible: false,
              builder: (context) {
                return AlertDialog(
                  title: Text("Give the code?"),
                  content: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: <Widget>[
                      TextField(
                        controller: _codeController,
                      ),
                    ],
                  ),
                  actions: <Widget>[
                    FlatButton(
                      child: Text("Confirm"),
                      textColor: Colors.white,
                      color: Colors.blue,
                      onPressed: () async {
                        final code = _codeController.text.trim();
                        AuthCredential credential =
                            PhoneAuthProvider.getCredential(
                                verificationId: verificationId, smsCode: code);

                        AuthResult result =
                            await _auth.signInWithCredential(credential);

                        FirebaseUser user = result.user;

                        if (user != null) {
                          Navigator.push(
                              context,
                              MaterialPageRoute(
                                  builder: (context) => HomeScreen(
                                        user: user,
                                      )));
                        } else {
                          print("Error");
                        }
                      },
                    )
                  ],
                );
              });
        },
        codeAutoRetrievalTimeout: null);
  }

错误:

errors.dart:163 Uncaught (in promise) Error: MissingPluginException(No implementation found for method verifyPhoneNumber on channel plugins.flutter.io/firebase_auth)
    at Object.throw_ [as throw] (errors.dart:212)
    at MethodChannel._invokeMethod (platform_channel.dart:157)
    at _invokeMethod.next (<anonymous>)
    at async_patch.dart:45
    at _RootZone.runUnary (zone.dart:1450)
    at _FutureListener.thenAwait.handleValue (future_impl.dart:143)
    at handleValueCallback (future_impl.dart:696)
    at Function._propagateToListeners (future_impl.dart:725)
    at _Future.new.[_completeWithValue] (future_impl.dart:529)
    at async._AsyncCallbackEntry.new.callback (future_impl.dart:567)
    at Object._microtaskLoop (schedule_microtask.dart:41)
    at _startMicrotaskLoop (schedule_microtask.dart:50)
    at async_patch.dart:166

由于我是Flutter的初学者,如果您有任何使用以下示例代码的方法:https://pub.dev/packages/firebase_phoneauth/install

谢谢。

1 个答案:

答案 0 :(得分:0)

_auth.verifyPhoneNumber() 仅适用于移动设备。 在 Flutter web 上使用 _auth.signInWithPhoneNumber() 进行电话身份验证。