简体   繁体   English

.然后代码在 flutter 中被忽略(不执行)

[英].then code is ignored(not executing )in flutter

I have 3 functions我有3个功能

  • 1.getAdharVerified() //gets adhar info from firebase and validates age later 1.getAdharVerified() //从firebase获取adhar信息并稍后验证年龄
  • registeElec() // register election details in firebase (adds data to firebase) registeElec() // 在 firebase 中注册选举细节(将数据添加到 firebase)
  • 3.createElection() //creates election in blockchain smart contract 3.createElection() //在区块链智能合约中创建选举

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我必须按照我有一个代码使用的相同顺序执行这些函数。然后在每个 function 之后但是。然后部分不执行

after executing first function it avoids then part and goes to last line在执行第一个 function 之后,它避免了然后部分并转到最后一行

here is my function这是我的 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记录 output 以便更好地理解

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.您拨打 Firebase 的电话是期货。 The.then statement says that when the future returns something, it executes that code. The.then 语句表示当 future 返回某些内容时,它会执行该代码。 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.但是,代码并没有停止,等待future 完成,它继续执行.then 之后的下一行代码。 In this scenario, I recommend you use asymc await, rather than.then clauses.在这种情况下,我建议您使用 asymc await,而不是 .then 子句。 The.then has its uses but probably not in this case. The.then 有它的用途,但在这种情况下可能没有。

Also, your try catch seems too low down.另外,您的 try catch 似乎太低了。 It needs to be able to catch an error from all of the firebase calls.它需要能够从所有 firebase 调用中捕获错误。

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

相关问题 Flutter - 使用 firebase 对查询执行查询 - Flutter - executing a query on a query with firebase 可以在 Firebase Analytics 上忽略从 Flutter 应用程序购买的沙盒吗? - Can sandbox purchases from Flutter app be ignored on Firebase Analytics? 在 DocumentSnapshot 的 get() 之后,程序没有执行 Flutter 中的下一行 - After get() of DocumentSnapshot, program is not executing next line in Flutter 执行整个代码后显示错误 - Showing the error after executing the entire code 在云函数中执行 python flask-assistant 代码时遇到问题 - Trouble executing python flask-assistant code in cloud function 在 shell 脚本中执行命令后如何跳过退出代码? - How to skip the exit code after executing a command in shell script? Flutter FirebaseAuth 未向手机发送验证码 - Flutter FirebaseAuth not sending verification code to phone Gradle 任务 assembleDebug 失败,退出代码为 1 flutter - Gradle task assembleDebug failed with exit code 1 flutter Flutter:Firebase 基本查询或基本搜索代码 - Flutter: Firebase basic Query or Basic Search code 我想在Flutter中使用firebase的十六进制代码 - I want to use a hexadecimal code from firebase in Flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM