当我在模拟器或真实设备(IPhone SE)上运行此代码时,当我按下"确认"按钮时,应用程序刚刚停止. 当我try 将一些断点置于调试模式时,它不会在断点处停止应用程序. 最后,在运行过程中,甚至当它冻结时,我都不会得到任何异常.

所以我请求您的帮助,提前谢谢您.

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)

推荐答案

您需要将Firebase URL方案添加到您的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>

你可以从你的GoogleService-Info.plist档案里拿到REVERSED_CLIENT_ID.

你可以找到更多信息.

Dart相关问答推荐

列表、捕获和...REST";元素的模式匹配

如何使用 http 库捕获 SocketException?

如何从 Polymer Dart 触发自定义事件?

Flutter Dismissible 坚持必须从树中删除列表项

如何编辑 Flutter 的 TextFormField 输入和 errorText 之间的间距

表单的 TextFormField 中的多个光标(cursor)

如何判断两张 map 的省道是否相等

如何在 Flutter 中使用 Provider 显示来自 ChangeNotifier 的错误

设置Dart中非常量的默认值

是否有必要为Flatter安装Android Studio,或者可以用什么替代Android Studio?

Flutter 使用什么 colored颜色 系统,为什么我们使用 `const Color` 而不是 `new Color`

Flutter:按下按钮时播放声音

GestureDetector onTap 卡

如何在 URL 中获取查询参数

polymer SEO 是否友好?

轻松判断数字是否在 Dart 的给定范围内?

dart中 library关键字的确切含义

启用空安全时,默认的List构造函数不可用.try 使用列表文字,List.filled或List.generate

如何展平flatten列表?

Dart 空值判断习惯用法或最佳实践是什么?