Firebase电话身份验证崩溃

时间:2018-09-12 01:55:37

标签: firebase dart firebase-authentication flutter

当我在模拟器或真实设备(iPhone SE)上运行此代码时,当我按下“确认”按钮时,该应用程序即停止运行。 当我尝试将某些断点置于调试模式时,它不会在断点处停止应用程序。 最后,在运行期间甚至冻结时,我都没有任何异常。

因此,请先谢谢您。

import 'dart:async';

import 'package:flutter/material.dart';

import 'package:firebase_auth/firebase_auth.dart';

import 'package:chart_test/testDeg.dart';
import 'main.dart';


class Login extends StatefulWidget {
  @override
  _LoginState createState() => new _LoginState();
}

class _LoginState extends State<Login> {
  String phoneNo;
  String smsCode;
  String verificationId;

  Future<void> verifyPhone() async {
    final PhoneCodeAutoRetrievalTimeout autoRetrieve = (String verId) {
      this.verificationId = verId;
    };

    final PhoneCodeSent smsCodeSent = (String verId, [int forceCodeResend]) {
      this.verificationId = verId;
      smsCodeDialog(context).then((value) {
        print('Signed in');
      });
    };

    final PhoneVerificationCompleted verifiedSuccess = (FirebaseUser user) {
      print('verified');
    };

    final PhoneVerificationFailed veriFailed = (AuthException exception) {
      print('${exception.message}');
    };

    await FirebaseAuth.instance.verifyPhoneNumber(
        phoneNumber: this.phoneNo,
        codeAutoRetrievalTimeout: autoRetrieve,
        codeSent: smsCodeSent,
        timeout: const Duration(seconds: 5),
        verificationCompleted: verifiedSuccess,
        verificationFailed: veriFailed);
  }

  Future<bool> smsCodeDialog(BuildContext context) {
    return showDialog(
        context: context,
        barrierDismissible: false,
        builder: (BuildContext context) {
          return new AlertDialog(
            title: Text('Enter sms Code'),
            content: TextField(
              keyboardType: TextInputType.number,
              onChanged: (value) {
                this.smsCode = value;
              },
            ),
            contentPadding: EdgeInsets.all(10.0),
            actions: <Widget>[
              new FlatButton(
                child: Text('Done'),
                onPressed: () {
                  FirebaseAuth.instance.currentUser().then((user) {
                    if (user != null) {
                      Navigator.of(context).pop();
                      Navigator.push(
                        context,
                        MaterialPageRoute(builder: (context) => TestDeg(user.phoneNumber,key:MyApp.link)),
                      );
                    } else {
                      Navigator.of(context).pop();
                      //signIn();
                    }
                  });
                },
              )
            ],
          );
        });
  }

  signIn() {
    FirebaseAuth.instance
        .signInWithPhoneNumber(verificationId: verificationId, smsCode: smsCode)
        .then((user) {
      Navigator.of(context).pushReplacementNamed('/homepage');
    }).catchError((e) {
      print(e);
    });
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('Connexion'),
      ),
      body: new Center(
        child: Container(
            padding: EdgeInsets.all(25.0),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                TextField(
                  decoration: InputDecoration(hintText: 'number'),
                  onChanged: (value) {
                    this.phoneNo = value;
                  },
                ),
                SizedBox(height: 10.0),
                RaisedButton(
                    onPressed: verifyPhone,
                    child: Text('Confirm'),
                    textColor: Colors.white,
                    elevation: 7.0,
                    color: Colors.blue)
              ],
            )),
      ),
    );
  }
}

第一个引发调用堆栈:

(
0   CoreFoundation                      0x00000001075521e6 __exceptionPreprocess + 294
1   libobjc.A.dylib                     0x00000001066ab031 objc_exception_throw + 48
2   CoreFoundation                      0x00000001075c7975 +[NSException raise:format:] + 197
3   Runner                              0x000000010246a5db -[FIRPhoneAuthProvider verifyPhoneNumber:UIDelegate:completion:] + 187
4   Runner                              0x00000001027e11af -[FLTFirebaseAuthPlugin handleMethodCall:result:] + 13919
5   Flutter                             0x00000001041defe3 __45-[FlutterMethodChannel setMethodCallHandler:]_block_invoke + 118
6   Flutter                             0x00000001041f90d0 _ZNK5shell21PlatformMessageRouter21HandlePlatfor<…>
Lost connection to device.
Exited (sigterm)

1 个答案:

答案 0 :(得分:7)

您需要将Firebase网址方案添加到Info.plist。转到<app_directory>/ios/Runner/Info.plist并添加以下内容:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    ...

    <key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleTypeRole</key>
            <string>Editor</string>
            <key>CFBundleURLSchemes</key>
            <array>
                <!-- Insert your reversed client ID here -->
                <string> REVERSED_CLIENT_ID </string>
            </array>
        </dict>
    </array>

    ...
</dict>
</plist>

您可以从REVERSED_CLIENT_ID文件中获取GoogleService-Info.plist

您可以找到更多信息here