dart语言学习3

1.遍历的四种方式
在这里插入图片描述
如果需要获取索引,那么就需要使用list.indexOf(i)进行获取索引值。
在这里插入图片描述
2.匿名函数,也成为函数的闭包。并且能够用胖箭头函数的形式进行定义。也称为lamda表达式形式。
在这里插入图片描述
在这里插入图片描述
3.运算符
a ? b: c
a is T
~/ 除号,但是返回值为整数,也就是向下取整
as 类型转换
is 如果对象是指定的类型返回 True
is! 如果对象是指定的类型返回 False 这个应该注意
使用 = 操作符来赋值。 但是还有一个 ??= 操作符用来指定 值为 null 的变量的值。
a = value; // 给 a 变量赋值
b ??= value; // 如果 b 是 null,则赋值给 b;
// 如果不是 null,则 b 的值保持不变
?. 条件成员访问 和 . 类似,但是左边的操作对象不能为 null,例如 foo?.bar 如果 foo 为 null 则返回 null,否则返回 bar 成员

4.continue的使用,但是好似没有什么意义

var command = ‘CLOSED’;
switch (command) {
case ‘CLOSED’:
executeClosed();
continue nowClosed;

nowClosed:
case ‘NOW_CLOSED’:
// Runs for both CLOSED and NOW_CLOSED.
executeNowClosed();
break;
}

5.assert 方法的参数可以为任何返回布尔值的表达式或者方法。 如果返回的值为 true, 断言执行通过,执行结束。 如果返回值为 false, 断言执行失败,会抛出一个异常 AssertionError)。

6.异常的处理。你可以使用on 或者 catch 来声明捕获语句,也可以 同时使用。使用 on 来指定异常类型,使用 catch 来 捕获异常对象。
函数 catch() 可以带有一个或者两个参数, 第一个参数为抛出的异常对象, 第二个为堆栈信息 (一个 StackTrace 对象)。

try {
breedMoreLlamas();
} on OutOfLlamasException {
// A specific exception
buyMoreLlamas();
} on Exception catch (e) {
// Anything else that is an exception
print(‘Unknown exception: $e’);
} catch (e) {
// No specified type, handles all
print(‘Something really unknown: $e’);
}finally{
}

猜你喜欢

转载自blog.csdn.net/yuezheyue123/article/details/83828961