Home > Back-end >  Flutter : want to excecute code in try-catch but async
Flutter : want to excecute code in try-catch but async

Time:12-16

hi everyone I want to execute some codes inside try-catch. if all conditions in try are met, then I want to change the screen. but because the conditions has await, the screen changes before the conditions are checked.

So is there a way to solve this? Is there anyway to wait for others to end and then do Navigator.push?

I tried Future.dowhile() but not working..

try {
        await savePhoneNum(_phoneNumberController.text);

        await postAppToken(
            tokenValue,
            _packageInfo.appName,
            _packageInfo.version,
            Theme.of(context).platform.toString().substring(15),
        _phoneNumberController.text);

        final PhoneAuthCredential credential = PhoneAuthProvider.credential(
        verificationId: _verificationId,
        smsCode: _smsController.text,
        );

        Navigator.push(
        context, MaterialPageRoute(builder: (context) => BottomBarScreen()));
        
    } catch (e) {
      
      ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('${e.toString()}')),
      );
    }

CodePudding user response:

final PhoneAuthCredential credential = PhoneAuthProvider.credential(
        verificationId: _verificationId,
        smsCode: _smsController.text,
        );

use then statement after the function calling

final PhoneAuthCredential credential = PhoneAuthProvider.credential(
        verificationId: _verificationId,
        smsCode: _smsController.text,
        )..then();

CodePudding user response:

you can use chain functions on async calls. for example await myFun1().then((result) => async callbackFunction).then((nextResult) => nextCallback) and so on. this way you make sure all your logic executes in correct way.

  • Related