简体   繁体   English

如何在 flutter 中的另一个异步 function 中调用一个异步 function

[英]How to call one async function inside another async function in flutter

I have two future functions one for verifying OTP with API and another for setting a password using the API.我有两个未来的功能,一个用于使用 API 验证 OTP,另一个用于使用 API 设置密码。

When the OTP verification is successful the set password API must be called. OTP验证成功后,必须调用设置密码API。 How can I do that?我怎样才能做到这一点?

My code is:我的代码是:

    () async {

var status = await registrationService.verifyOtp(registrationData.mobileNumber,otpController.text);
                                if(status == 'approved'){
                                  print('success');
                                      () async {
                                    var passwordStatus = await registrationService.setPassword(registrationData.name, registrationData.number, passController.text);
                                    if(passwordStatus == 'approved'){
                                      print('approved');
                                      Navigator.pushNamed(context, StudMainPage.id);
                                    }
                                    else{
                                      WidgetsBinding.instance.addPostFrameCallback((timeStamp) => _showNewVersionAvailableDialog(context));
                                    }
                                  };
                                 }

    }

It shows error or the function is not being called.它显示错误或未调用 function。 What should I do now?我现在该怎么办? Thanks in advance.提前致谢。

Use then Callback then使用回调

registrationService.verifyOtp(registrationData.mobileNumber,otpController.text).then((status) {

if(status == 'approved'){
   print('success');

registrationService.setPassword(registrationData.name, registrationData.number, passController.text).then((response) {

//....
})))

} }

Create a second async function for创建第二个异步 function

Future<PasswordStatus> getPasswordStatus() async {
await registrationService.setPassword(registrationData.name, registrationData.number, passController.text);
}

and call that function as;并将其称为 function ;

if(status == 'approved'){
   var passwordStatus = await getPasswordStatus();
}

then just use the passwordStatus value.然后只需使用 passwordStatus 值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM