Johan Walhout
Johan Walhout

Reputation: 1656

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

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.

Upvotes: 6

Views: 3440

Answers (1)

jamesdlin
jamesdlin

Reputation: 90125

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

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

Upvotes: 4

Related Questions