dartfor
import 'dart:io';
void main(List<String> args) {
var message = ['map', 'list', 'set'];
for (var i = 0; i < 2; i++) {
print(message[i]);
}
// 捕获异常
var callbacks = [];
for (var i = 0; i < 2; i++) {
callbacks.add(() => print(i));
}
print(callbacks);
for (final c in callbacks) {
print('------');
c();
}
// for-in 一个 list 或者set
var obj = [12, 13, 14];
for (var prop in obj) {
print(prop);
}
// while循环
var num = 5;
while (num >= 2) {
num--;
print("num : ${num}");
}
// while 循环会在执行循环体前先判断条件:
// while (!isDone()) {
// doSomething();
// }
//do-while 循环则会 先执行一遍循环体 再判断条件:
do {
doSomething();
} while (!isDone());
// forEach
var collection = [1, 2, 3];
collection.forEach((v) => print('forEach.value : ${v}')); // 1 2 3
// forEach map
var userMap = {"name": "Maxsu", 'Email': 'maxsu@yiibai.com'};
userMap.forEach((k, v) => print('${k}: ${v}'));
}
isDone() {
return true;
}
doSomething() {
sleep(Duration(seconds: 1));
print('while don\'t stop');
}
本文由 李阿斗创作 旅途愉快~ 我的主页 ( ̄▽ ̄)
最后编辑时间为:
2023/04/30 01:49
这篇文章写得深入浅出,让我这个小白也看懂了!
看 dart呢