Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Dart equivalent of Python's all() method?

Tags:

dart

I'm looking for a method that returns true if a function test, given an element, returns true for every element of an Iterable, or in my case a List, similar to Python's all() method.

like image 893
Michael Pfaff Avatar asked Jan 22 '26 20:01

Michael Pfaff


2 Answers

I think what you are looking is the every method.

  var numbers = [1, 2, 3, 4, 5];
  var evenNumbers = [2, 4, 6, 8, 10];

  print(numbers.every((n) => n.isEven)); //false
  print(evenNumbers.every((n) => n.isEven)); //true
like image 69
Mattia Avatar answered Jan 25 '26 16:01

Mattia


Iterable#every

Checks whether every element of this iterable satisfies test.

void main() {
  print([1, 2, 3].every((i) => i > 0));
}
like image 40
SpencerPark Avatar answered Jan 25 '26 16:01

SpencerPark



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!