3

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.

julemand101
  • 21,132
  • 4
  • 38
  • 37
Johan Walhout
  • 1,166
  • 2
  • 20
  • 35
  • 1
    Even in Python, `(a or b or c) > 0` works only in *that particular case*. It wouldn't work if your comparison were, say, `> 1`, or if `a` were `-1`. IMO `(a or b or c) > 0` would be dangerously misleading. – jamesdlin Sep 10 '20 at 10:54

1 Answers1

2

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

if ([a, b, c].any((x) => x > 0)) {
  print('Yeh!');
}
jamesdlin
  • 65,531
  • 13
  • 129
  • 158