前提・実現したいこと
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.
のようなエラーが発生します。
自分のコード
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(),
// initialRoute: '/',
// routes: {
// '/' : (context) => WelcomeClass(),
// '/LoginClass' : (context) => LoginClass(),
// '/SignUpClass' : (context) => SignUpClass(),
// },
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
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 verificationSuccess = (AuthCredential user){
print('Phone Verification Completed');
};
final PhoneVerificationFailed verificationFailed =
(AuthException exception) {
print('${exception.message}');
};
await FirebaseAuth.instance.verifyPhoneNumber(
phoneNumber: this.phoneNo,
codeAutoRetrievalTimeout: autoRetrieve,
codeSent: smsCodeSent,
timeout: const Duration(seconds: 60),
verificationCompleted: verificationSuccess,
verificationFailed: verificationFailed,
);
}
Future<bool> smsCodeDialog(BuildContext context) {
return showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return new AlertDialog(
title: Text('Enter sms code'),
content: TextField(
onChanged: (value) {
this.smsCode = value;
},
),
contentPadding: EdgeInsets.all(10),
actions: <Widget>[
FlatButton(
child: Text('Done'),
onPressed: () {
FirebaseAuth.instance.currentUser().then((user) {
if (user != null) {
Navigator.of(context).pop();
Navigator.of(context)
.pushReplacementNamed('/SignUpClass');
} else {
Navigator.of(context).pop();
signIn();
}
});
},
)
],
);
});
}
signIn() {
final AuthCredential credential = PhoneAuthProvider.getCredential(
verificationId: verificationId, smsCode: smsCode);
FirebaseAuth.instance.signInWithCredential(credential).then((user) {
Navigator.of(context).pushReplacementNamed('/homepage');
}).catchError((e) {
print('Auth Credential Error : $e');
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Padding(
padding: const EdgeInsets.all(38.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
TextField(
onChanged: (value) {
this.phoneNo = value;
},
),
SizedBox(
height: 40,
),
FlatButton(
child: Text("Send code"),
onPressed: verifyPhone,
), //FlatButton
], // Widget
),
), // Column
);
}
}
本当に困っています。よろしくお願いします。
補足情報(FW/ツールのバージョンなど)
- Android Studio
- Dart
- Firebase Authentication
マルチポスト先: