Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I want to check condition for all for loop iteration and after that statement will execute

I want to check condition for all for loop iteration and after that statement will execute. if condition was false for any iteration then statement should not execute.

void _validateInputs() {
    if (_count == 0 && _formKey1.currentState.validate()) {
      _register(context);
    } else {
      for (int i = 0; i < _formKeys.length - 1; i++) {
       
       if (_formKeys[i].currentState.validate()) {
                  

          print("hello world ");
        } else {
          print("hello ");
          return;
        }
      }
    }
  

}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Create a new variable to hold if it is valid for all iterations or not. And then, once the iterations are over, Execute the corresponding statements.

Here for each iteration, check if the conditions are not met, And if it is not met even for a single iteration, Update the isValid variable to false and then break out of the loop.

bool isValid = true;

  if (_count == 0 && _formKey1.currentState.validate()) {
    _register(context);
  } else {
    for (int i = 0; i < _formKeys.length - 1; i++) {
      if (!(_formKeys[i].currentState.validate())) {
        isValid = false;
        break;
      }
    }

    // CHECK HERE
    if (isValid) {
      print("hello world ");
    } else {
      print("hello ");
    }
  }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading