简体   繁体   English

返回不在 For 循环 Flutter/Dart 内工作

[英]Return Not Working Inside For Loop Flutter/Dart

I'm making a registry page in an app I'm building.我正在我正在构建的应用程序中制作一个注册表页面。 My issue is the logic that checks the database to see if an existing user already has the username that they are trying to register with.我的问题是检查数据库以查看现有用户是否已经拥有他们尝试注册的用户名的逻辑。 I get all the usernames in the database and loop through them in a for loop, and have an if statement to see if any match, heres the relevant code:我获取数据库中的所有用户名并在 for 循环中遍历它们,并有一个 if 语句来查看是否有任何匹配,以下是相关代码:

class RegistryValidator{

  static ErrorReporter validate(String userName, String password, String email){

    Services.getUserName().then((value) {//value = list<User> where User is a custom object
       for(var i = 0; i < value.length; i++){
         print(value.elementAt(i).userName);
         if (value.elementAt(i).userName == userName){
            print('FOUND A DUPLICATE'); //This prints
            return new ErrorReporter("Failure", "UserName already exists");
         // loop ends here and continues to else block
         }
       }
    });
   if (userName.length < 3){
     return new ErrorReporter("Failure", "UserName needs to be at least 3 characters long");
   }
   else if(userName.length > 20){
     return new ErrorReporter("Failure", "UserName needs to be at under 20 characters long");
   }
   else if(password.length < 8){
     return new ErrorReporter("Failure", "Password needs to be at least 8 characters long");
   }
   else if(password.length > 20){
     return new ErrorReporter("Failure", "Password needs to be at under 20 characters long");
   }
   else if(email.length > 50){
     return new ErrorReporter("Failure", "Choose a shorter email. We do not accept emails longer than 50 characters");
   }
   else if(!EmailValidator.validate(email)){
     return new ErrorReporter("Failure", "Invalid Email, please enter a valid email");
   }
   else{
     return new ErrorReporter("Success", "You Are Registered!");
   }
 }
}

The ErrorReporter class: ErrorReporter 类:

String status;
String errorDesc = "";
ErrorReporter(this.status, this.errorDesc);

and finally, inside of the onPressed() I have:最后,在 onPressed() 里面我有:

ErrorReporter reporter = RegistryValidator.validate(userNameController.text, passwordController.text, emailController.text);

For example, if I were to input a username that already exists within the database, calling reporter.status should give me "Failure", but I get "Success" everytime.例如,如果我要输入数据库中已经存在的用户名,调用reporter.status应该给我“失败”,但每次我都会得到“成功”。

NOTE: I'm getting the expected behavior from the other if statements.注意:我从其他 if 语句中得到了预期的行为。 However when a duplicate is found via for loop, the return only seems to work for exiting out of the for loop and not the function.但是,当通过 for 循环找到重复项时,返回似乎只适用于退出 for 循环而不是函数。 Could anyone help me understand why it's acting this way?谁能帮我理解为什么它会这样?

Thanks to @jamesdlin, I figured out what I was doing wrong.感谢@jamesdlin,我发现我做错了什么。 First off, the reason the return call wasn't working in the for loop was because I was returning to the .then() call instead of .getUserName() , but even after fixing that up, I still wasn't getting the right results.首先,将原因return电话没有在for循环中,因为我回到工作.then()调用,而不是.getUserName()但即使是固定后,我仍然没有得到正确的结果。 That's because I wasn't waiting for the Future/function to finish.那是因为我没有等待 Future/function 完成。 This is accomplished by using the await keyword before calling your Future.这是通过在调用 Future 之前使用await关键字来完成的。 For anyone else that doesn't quite understand this yet, I found this site very helpful: https://dart.dev/codelabs/async-await对于还不太了解这一点的其他人,我发现这个网站非常有帮助: https : //dart.dev/codelabs/async-await

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

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