前提・実現したいこと
Flutter のアプリでFirebaseのAuthenticationを利用した電話番号認証をしたい。
発生している問題・エラーメッセージ
android studio を使っています。
認証ボタンを押した瞬間に
Xcode build done. 21.5s path: satisfied (Path is satisfied), interface: en0 Configuring the default Firebase app... Configured the default Firebase app __FIRAPP_DEFAULT. path: satisfied (Path is satisfied), interface: en0 Syncing files to device iPhone 11 Pro Max... *** First throw call stack: ( 0 CoreFoundation 0x00007fff23c7127e __exceptionPreprocess + 350 1 libobjc.A.dylib 0x00007fff513fbb20 objc_exception_throw + 48 2 CoreFoundation 0x00007fff23c710bc +[NSException raise:format:] + 188 3 Runner 0x0000000103e97d81 -[FIRPhoneAuthProvider verifyPhoneNumber:UIDelegate:completion:] + 193 4 Runner 0x000000010440dc3d -[FLTFirebaseAuthPlugin handleMethodCall:result:] + 18109 5 Flutter 0x000000010600ddb5 __45-[FlutterMethodChannel setMethodCallHandler:]_block_invoke + 104 6 Flutter 0x0000000105fa1ba0 _ZNK7flutter21PlatformMessageRouter21HandlePlatf<…> Lost connection to device.
のようなエラーが発生します。
自分のコード
dart
1import 'package:flutter/material.dart'; 2 3import 'package:firebase_auth/firebase_auth.dart'; 4 5 6void main() => runApp(MyApp()); 7 8class MyApp extends StatelessWidget { 9 10 Widget build(BuildContext context) { 11 return MaterialApp( 12 home: MyHomePage(), 13 14// initialRoute: '/', 15// routes: { 16// '/' : (context) => WelcomeClass(), 17// '/LoginClass' : (context) => LoginClass(), 18// '/SignUpClass' : (context) => SignUpClass(), 19// }, 20 ); 21 } 22} 23 24class MyHomePage extends StatefulWidget { 25 26 _MyHomePageState createState() => _MyHomePageState(); 27} 28 29class _MyHomePageState extends State<MyHomePage> { 30 String phoneNo; 31 String smsCode; 32 String verificationId; 33 34 Future<void> verifyPhone() async { 35 final PhoneCodeAutoRetrievalTimeout autoRetrieve = (String verId) { 36 this.verificationId = verId; 37 }; 38 39 final PhoneCodeSent smsCodeSent = (String verId, [int forceCodeResend]) { 40 this.verificationId = verId; 41 smsCodeDialog(context).then((value){ 42 print('signed in'); 43 }); 44 }; 45 46 final PhoneVerificationCompleted verificationSuccess = (AuthCredential user){ 47 print('Phone Verification Completed'); 48 }; 49 50 final PhoneVerificationFailed verificationFailed = 51 (AuthException exception) { 52 print('${exception.message}'); 53 }; 54 55 await FirebaseAuth.instance.verifyPhoneNumber( 56 phoneNumber: this.phoneNo, 57 codeAutoRetrievalTimeout: autoRetrieve, 58 codeSent: smsCodeSent, 59 timeout: const Duration(seconds: 60), 60 verificationCompleted: verificationSuccess, 61 verificationFailed: verificationFailed, 62 ); 63 } 64 65 Future<bool> smsCodeDialog(BuildContext context) { 66 return showDialog( 67 context: context, 68 barrierDismissible: false, 69 builder: (BuildContext context) { 70 return new AlertDialog( 71 title: Text('Enter sms code'), 72 content: TextField( 73 onChanged: (value) { 74 this.smsCode = value; 75 }, 76 ), 77 contentPadding: EdgeInsets.all(10), 78 actions: <Widget>[ 79 FlatButton( 80 child: Text('Done'), 81 onPressed: () { 82 FirebaseAuth.instance.currentUser().then((user) { 83 if (user != null) { 84 Navigator.of(context).pop(); 85 Navigator.of(context) 86 .pushReplacementNamed('/SignUpClass'); 87 } else { 88 Navigator.of(context).pop(); 89 signIn(); 90 } 91 }); 92 }, 93 ) 94 ], 95 ); 96 }); 97 } 98 99 signIn() { 100 final AuthCredential credential = PhoneAuthProvider.getCredential( 101 verificationId: verificationId, smsCode: smsCode); 102 103 FirebaseAuth.instance.signInWithCredential(credential).then((user) { 104 Navigator.of(context).pushReplacementNamed('/homepage'); 105 }).catchError((e) { 106 print('Auth Credential Error : $e'); 107 }); 108 } 109 110 Widget build(BuildContext context) { 111 return Scaffold( 112 body: Padding( 113 padding: const EdgeInsets.all(38.0), 114 child: Column( 115 crossAxisAlignment: CrossAxisAlignment.start, 116 children: <Widget>[ 117 TextField( 118 onChanged: (value) { 119 this.phoneNo = value; 120 }, 121 ), 122 SizedBox( 123 height: 40, 124 ), 125 FlatButton( 126 child: Text("Send code"), 127 onPressed: verifyPhone, 128 ), //FlatButton 129 ], // Widget 130 ), 131 ), // Column 132 ); 133 } 134} 135 136 137
本当に困っています。よろしくお願いします。
補足情報(FW/ツールのバージョンなど)
Android Studio
Dart
Firebase Authentication
