簡體   English   中英

未處理的異常:類型 &#39;(String, int) =&gt; Future<Null> &#39; 不是類型轉換中類型 &#39;(String, int?) =&gt; void&#39; 的子類型

[英]Unhandled Exception: type '(String, int) => Future<Null>' is not a subtype of type '(String, int?) => void' in type cast

需要幫助來解決這個問題,我在運行我的代碼時遇到了這個問題。 它不斷顯示此錯誤未處理的異常:類型'(字符串,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, }); } }

改變

final PhoneCodeSent smsOtpSend = (String verId, int resendToken) async {
      this.verificationId = verId;
    }

final PhoneCodeSent smsOtpSend = (String verId, int? resendToken) {
      this.verificationId = verId;
    }

回調類型是int? 不是int ,請注意空安全的問號。 並刪除async關鍵字,因為它是一個常規函數,而不是未來。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM