Home > Net >  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

Time:11-10

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;
        }
      }
    }
  

}

CodePudding user response:

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 ");
    }
  }
  • Related