简体   繁体   中英

.then code is ignored(not executing )in flutter

I have 3 functions

  • 1.getAdharVerified() //gets adhar info from firebase and validates age later
  • registeElec() // register election details in firebase (adds data to firebase)
  • 3.createElection() //creates election in blockchain smart contract

I have to execute these functions in the same order I have a code using.then after every function but.then part is not executing

after executing first function it avoids then part and goes to last line

here is my function

void startElectionComplete() {
    // the code to start election from adhar verification,register election and create election at blockchain

    if (kDebugMode) {print('verifying adhar');}
    getAdharVerified(adharTextController.text).then((value) => (){ //ADHAR VERIFICATION FUNCTION
      showSnackBar(snackbarshow().succesAdharSnack);

      if (kDebugMode) {print('adhar verified');}
      if (_adharage > 18 && privateKeyTextController.text.isNotEmpty) {  // CHECKING AGE FROM ADHAR

        if (kDebugMode) {print('adhar verification complete');}  // CHECKING WEATHER ELECTION DATES ARE GIVEN
        if(unixlast != null &&unixlast.isNotEmpty){
          if(unix !=null && unix.isNotEmpty){
            if (kDebugMode) {print('unix not nulll');}

            try{
              if (kDebugMode) {print('registering');}
              registerElec(unix,unixlast).then((value) => ()  {// REGISTERING THE ELECTION IN FIREBASE
                if (kDebugMode) {print('creating blockchain');}
                //AFTER REGISTRATION CREATING ELECTION ON BLOCKCHAN
                createElection(electionNameTextController.text, ethclient!, privateKeyTextController.text, contractAdressConst);
                showSnackBar(snackbarshow().succesAdharSnack);
              });
              gotoPickElec();
            }catch(e){
              if (kDebugMode) {
                print(e);
              }

              showSnackBar(errorAdharSnack);
            }if (kDebugMode) {print('there is a problemmm');}
          }showSnackBar(errorSnack);
        }showSnackBar(errorSnack);
      }showSnackBar(errorSnack);
    });print('i am now out of then part');

  }

log output for better understanding

I/flutter ( 6105): the unix time stamp is 1672770600
I/flutter ( 6105): the unix time stamp is 1672857000
I/flutter ( 6105): verifying adhar
I/flutter ( 6105): i am now out of then part

I don't understand what wrong happened here is there any way to do this in less time and more faster way? please explain what happened so that I can understand what is going on here,thankyou

Your calls to Firebase are futures. The.then statement says that when the future returns something, it executes that code. So far so good,. However, the code doesn't stop, waiting for the future to complete, it continues to the next line of code after the.then. In this scenario, I recommend you use asymc await, rather than.then clauses. The.then has its uses but probably not in this case.

Also, your try catch seems too low down. It needs to be able to catch an error from all of the firebase calls.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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