Dart by Example: For

  1. main() {
  2. // A standard for loop
  3. for (var i = 0; i < 3; i++) {
  4. print(i);
  5. }
  6. // 'for-in' can be used on any class that implements Iterable
  7. var collection = [3, 4, 5];
  8. for (var x in collection) {
  9. print(x);
  10. }
  11. // Closures will capture the value of the index (Dart is lexically scoped)
  12. var callbacks = [];
  13. for (var i = 6; i < 8; i++) {
  14. callbacks.add(() => print(i));
  15. }
  16. callbacks.forEach((c) => c()); // invoke each callback
  17. }
  18.  
  1. $ dart for.dart
  2. 0
  3. 1
  4. 2
  5. 3
  6. 4
  7. 5
  8. 6
  9. 7

by @jryanio | source | license