在我的顫振應用程式中,我有一個延遲 5 秒來激活按鈕的功能。當我導航到其他頁面時,即使我使用“pushReplacement”導航器,延遲的計時器仍在作業。當我導航到其他頁面時,誰能幫我找到一種方法來處理或取消這個計時器。
這是代碼:
Future sendVerificationEmail() async {
try{
final user =FirebaseAuth.instance.currentUser!;
await user.sendEmailVerification();
setState(() => canResendEmail = false);
await Future.delayed(const Duration(seconds: 5)); // this is the line causing the error
setState(() => canResendEmail = true);
}catch (e) {
Utils.showSnackBar(e.toString());
}
}
這是導航按鈕功能:
Future<void> SignOut() async {
await FirebaseAuth.instance.signOut();
Navigator.pushReplacement(
context,MaterialPageRoute(builder: (context) => mainPage()),
);
}
uj5u.com熱心網友回復:
嘗試改用計時器
Timer timer = Timer(Duration(seconds: 5), () {
//do something here();
});
// You can dispose the timer like this
timer.cancel();
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/498230.html
