需要幫助來解決這個問題,我在運行我的代碼時遇到了這個問題。它一直顯示這個錯誤未處理的例外:型別'(字串,int)=>未來'不是型別轉換中的'(字串,int?)=> void'型別的子型別..我嘗試解決這個問題無濟于事,我去了各種論壇,看到了不同的想法,但這些想法并不適用。我希望有人可以幫助我。
class AuthProvider with ChangeNotifier {
FirebaseAuth _auth = FirebaseAuth.instance;
late String smsOtp;
late String verificationId;
String error = '';
UserServices _userServices = UserServices();
Future < void > verifyPhone(BuildContext context, String number) async {
final PhoneVerificationCompleted verificationCompleted =
(PhoneAuthCredential credential) async {
await _auth.signInWithCredential(credential);
};
final PhoneVerificationFailed verificationFailed = (FirebaseAuthException e) {
print(e.code);
};
final PhoneCodeSent smsOtpSend = (String verId, int resendToken) async {
this.verificationId = verId;
}
as PhoneCodeSent;
smsOtpDialog(context, number);
try {
_auth.verifyPhoneNumber(
phoneNumber: number,
verificationCompleted: verificationCompleted,
verificationFailed: verificationFailed,
codeSent: smsOtpSend,
codeAutoRetrievalTimeout: (String verId) {
this.verificationId = verId;
},
);
} catch (e) {
print(e);
}
}
Future < bool ? > smsOtpDialog(BuildContext context, String number) {
return showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Column(
children: [
Text('Verification Code'),
SizedBox(height: 6, ),
Text('Enter the 6 didgit Code received by sms',
style: TextStyle(color: Colors.grey, fontSize: 10),
),
],
),
content: Container(
height: 85,
child: TextField(
textAlign: TextAlign.center,
keyboardType: TextInputType.number,
maxLength: 6,
onChanged: (value) {
this.smsOtp = value;
},
),
),
actions: [
FlatButton(
onPressed: () async {
(context);
try {
PhoneAuthCredential phoneAuthCredential =
PhoneAuthProvider.credential(
verificationId: verificationId,
smsCode: smsOtp,
);
final User ? user = (await _auth.signInWithCredential(phoneAuthCredential)).user;
//create user data in fireStore after successful login
_createUser(id: user!.uid, number: user.phoneNumber ? ? "");
//navigate to homepage after login
if (user != null) {
Navigator.of(context).pop();
//dont want to come back to welcome screen after login
Navigator.of(context).pushReplacement(MaterialPageRoute(
builder: (context) => HomeScreen(),
));
} else {
print('Login Failed');
}
} catch (e) {
this.error = 'Invalid Code';
print(e.toString());
notifyListeners();
Navigator.of(context).pop();
}
},
child: Text('DONE', style: TextStyle(color: Theme.of(context).primaryColor), ),
),
],
);
});
}
void _createUser({
required String id,
required String number
}) {
_userServices.createUserData({
'id': id,
'number': number,
});
}
}
uj5u.com熱心網友回復:
改變
final PhoneCodeSent smsOtpSend = (String verId, int resendToken) async {
this.verificationId = verId;
}
到
final PhoneCodeSent smsOtpSend = (String verId, int? resendToken) {
this.verificationId = verId;
}
回呼型別int?不是int,注意空安全的問號。并洗掉async關鍵字,因為它是一個常規函式,而不是未來。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/330701.html
上一篇:type'(BuildContext,Widget)=>ChangeNotifierProvider<>'不是型別轉換中型別'(BuildContext
下一篇:在Flutter中創建復選框
