How to compare multiple values in an if statement at once in Dart

Viewed 1484

I have multiple variables which needs the same check in a if-statement in Dart. I need to know if there is at least one of the variables > 0.

For example:

var a = 1;
var b = 0;
var c = 0;

if ((a > 0) || (b > 0) || (c > 0)) {
  print('Yeh!');
}

This should be done easier, like in Python.

The following code isn't valid, but I tried this:

if ((a || b || c) > 0) {
  print('Yeh!');
}

Any tips would be nice.

1 Answers

One way would be to create a List and to use Iterable.any:

if ([a, b, c].any((x) => x > 0)) {
  print('Yeh!');
}
Related