级别: ★☆☆☆☆
标签:「Flutter 」「Dart」「Dart Operator」「Dart Exceptions」
做者: WYW
审校: QiShare团队php
前言
笔者在以前已经写了2篇Dart的基础文章了。
Dart 基础 (一) Dart 基础 (二)html
本文是Dart基础的第3篇,在本文中,笔者会主要介绍2部份内容,运算符和异常。git
详情以下:github
Dart中可能遇到的运算符以下图所示:算法
上述运算符中,笔者不大熟悉的运算符有:bash
~/
??
..
as
、is
、is!
?.
若是你对其余运算符不大熟悉,能够查看Dart文档。微信
~/
: 整除;整除的结果是 运算符左侧的数 除以 运算符右侧的数 能够商几。网络
5 ~/ 2 = 2;
7 ~/ 3 = 2;
9 ~/ 3 = 0;
复制代码
??
String qiShare1 = 'qiShare1';
String qiShare2;
qiShare2 ??= qiShare1;
print(qiShare2);
// 输出结果
qiShare1
复制代码
..
..
级联运算符ide
class QiCascade {
String firstProperty;
String secondProperty;
String thirdProperty;
String fourthProperty;
}
class QiSubCascade extends QiCascade{
}
void main() {
QiCascade cascade = QiCascade();
cascade.firstProperty = 'firstPropertyValue';
cascade.secondProperty = 'secondPropertyValue';
cascade.thirdProperty = 'thirdPropertyValue';
cascade.fourthProperty = 'fourthPropertyValue';
print('输出属性:${cascade..firstProperty ..secondProperty ..thirdProperty ..fourthProperty}');
print('级联输出:');
print(cascade..firstProperty..secondProperty..thirdProperty..fourthProperty);
print('属性:${cascade.firstProperty}');
print(cascade.firstProperty);
print(cascade.secondProperty);
print(cascade.thirdProperty);
print(cascade.fourthProperty);
cascade..firstProperty = 'changedFirstPropertyValue'
..secondProperty = 'changedSecondPropertyValue'
..thirdProperty = 'changedThirdPropertyValue'
..fourthProperty = 'changedFourthPropertyValue';
print('级联输出:${cascade..firstProperty ..secondProperty ..thirdProperty ..fourthProperty}');
}
复制代码
输出结果学习
flutter: 输出属性:Instance of 'QiCascade'
flutter: 级联输出:
flutter: Instance of 'QiCascade'
flutter: 属性:firstPropertyValue
flutter: firstPropertyValue
flutter: secondPropertyValue
flutter: thirdPropertyValue
flutter: fourthPropertyValue
flutter: 级联输出:Instance of 'QiCascade'
复制代码
看起来级联运算符能够用于同时操做并列的实例变量。
as
、is
、is!
操做符 | 解释 |
---|---|
as | 类型转换 |
is | 若是对象是指定的类型返回true |
is! | 若是对象是指定的类型返回false |
dynamic subCascade = QiSubCascade();
if (subCascade is QiCascade) {
subCascade.firstProperty = 'isQiCascadeFirstPropertyValue';
}
print('subCascade属性:${subCascade.firstProperty}');
print('subCascade runtimeType:${subCascade.runtimeType}');
if(subCascade.runtimeType == QiSubCascade) {
print('subCascade的runtimeType为 ${subCascade.runtimeType}');
}
(subCascade as QiCascade).firstProperty = 'asQiCascadeFirstPropertyValue';
print('subCascade属性:${subCascade.firstProperty}');
复制代码
使用 is 和 as 的区别在于:
- 使用is:若是上述subCascade不是QiCascade,则条件中的赋值代码不会执行
- 使用as:若是上述subCascade为null 或者不是QiCascade类型,则运行过程当中会抛出异常。
输出结果
flutter: subCascade属性:isQiCascadeFirstPropertyValue
flutter: subCascade runtimeType:QiSubCascade
flutter: subCascade的runtimeType为 QiSubCascade
flutter: subCascade属性:asQiCascadeFirstPropertyValue
复制代码
运算符 | 名字 | 解释 |
---|---|---|
() | 使用方法 | 表明调用一个方法。 |
[] | 访问List | 访问list 中特定位置的元素。 |
. | 访问Member | 访问元素,如上边咱们访问cascade.firstProperty。 |
?. | 条件成员访问 | 和 . 类型, 可是.左边操做对象不能为null,不然抛出异常,?.左边的操做对象能够为null,返回null。 |
subCascade = null;
try {
print('赋值null 后访问成员 ${subCascade.firstProperty}');
} catch (e) {
print('异常信息 $e');
}
print('赋值null 后访问成员 ${subCascade?.firstProperty}');
复制代码
输出结果
flutter: 异常信息 NoSuchMethodError: The getter 'firstProperty' was called on null.
Receiver: null
Tried calling: firstProperty
flutter: 赋值null 后访问成员 null
复制代码
若是咱们使用条件成员访问运算符?.
。就不会有上述异常。
其余运算符:?.
: 条件成员访问,若是操做符左侧的实例存在,则会取值 ;
如qiShare?.name,若是qiShare 不为null,则返回结果为qiShare.name。不然返回结果为null。
subCascade = null;
subCascade ?. firstProperty;
复制代码
常见的异常有 FormatException格式异常、HttpException网络异常、FileSystemException操做文件的异常、越界的异常,操做的实例调用了没有实现的方法 的异常。
try 用于包含可能出现异常的代码
throw 用于抛出异常。
Catch 用于捕获异常,能够防止异常继续传递。除非使用了rethrow
会将捕获的异常再次抛出。
笔者先举了2个特定的异常例子FormatException 、IntegerDivisionByZeroException
1.FormatException,在把字符串'1234B'转为数字的时候出现的类型转换异常。
var numValue = '1234B';
try {
int numValueInt = int.parse(numValue);
print(numValueInt);
} on FormatException catch (e){
print('出现FormatException: $e');
// rethrow; 使用rethrow 会将catch 住的异常再次抛出
} on Exception catch(e) {
print('Exception: $e');
// rethrow; 使用rethrow 会将catch 住的异常再次抛出
}
// 输出结果:
/*
flutter: 出现FormatException: FormatException: Invalid radix-10 number (at character 1)
123\^]4B
*/
复制代码
2.IntegerDivisionByZeroException 在0做除数的时候出现的异常。整除出现。
// double zeroValue = 0.0; // 若是使用0.0 则IntegerDivisionByZeroException 不会捕获
int zeroValue = 0;
int num1 = 1;
try {
print(num1 ~/ zeroValue); // 会触发异常 可是也不是除0异常
// print(num1 / zeroValue); // 不会触发异常
} on IntegerDivisionByZeroException catch(e) {
print('除以0异常:$e');
} catch (e) {
print('异常信息:$e');
}
// 输出结果
flutter: 除以0异常:IntegerDivisionByZeroException
复制代码
下边笔者又列举了其余的几个异常的例子。
// 抛出异常示例
try {
throw Exception(
'Custom Exception'
);
} catch (e) {
print(e);
}
try {
throw '自定义字符串Exception';
} catch (e) {
print(e);
}
List list1 = ['QiShare'];
try {
print(list1[1]);
} catch (e) {
print(e);
}
try {
(list1 as QiCascade).firstProperty;
} catch (e) {
print(e);
}
list1 = null;
try {
print(list1[1]);
} catch (e) {
print(e);
}
try {
(list1 as QiCascade).firstProperty;
} catch (e) {
print(e);
}
复制代码
输出结果
flutter: Exception: Custom Exception
flutter: 自定义字符串Exception
flutter: RangeError (index): Invalid value: Only valid value is 0: 1
flutter: type 'List<dynamic>' is not a subtype of type 'QiCascade' in type cast
flutter: NoSuchMethodError: The method '[]' was called on null.
Receiver: null
Tried calling: [](1)
flutter: NoSuchMethodError: The getter 'firstProperty' was called on null.
Receiver: null
Tried calling: firstProperty
复制代码
针对上述代码的异常捕获,笔者发现,catch不只能够捕获异常也能够捕获Error,笔者Dart 的Exceptions 包括Exception 和 Error。而且对如上代码中RangeError、NoSuchMethodError的代码作了以下处理:
捕获RangeError
List list1 = ['QiShare'];
try {
print(list1[1]);
} on RangeError catch(error) {
print('RangeError错误:$error');
} catch (e) {
print(e.runtimeType);
print(e);
}
// 输出结果:
/*
flutter: RangeError错误:RangeError (index): Invalid value: Only valid value is 0: 1
*/
复制代码
捕获NoSuchMethodError
List list1;
try {
print(list1[1]);
} on NoSuchMethodError catch(noSuchMethodError){
print('NoSuchMethodError错误:$noSuchMethodError');
} catch (e) {
print('异常信息:$e');
}
// 输出结果:
/**
* flutter: NoSuchMethodError错误:NoSuchMethodError: The method '[]' was called on null.
Receiver: null
Tried calling: [](1)
*/
复制代码
Finally 部分的代码,无论是否有出现异常,都会执行,若是出现了异常,则执行完catch中的代码后,会执行Finally 中的代码,若是没有出现异常,则执行完了try中的代码后,会执行Finally 中的代码。
List list1;
try {
print(list1[1]);
} on NoSuchMethodError catch(noSuchMethodError){
print('NoSuchMethodError错误:$noSuchMethodError');
} catch (e) {
print('异常信息:$e');
} finally {
print('执行Finally 中的代码');
}
/**
* flutter: NoSuchMethodError错误:NoSuchMethodError: The method '[]' was called on null.
Receiver: null
Tried calling: [](1)
flutter: 执行Finally 中的代码
*/
复制代码
还有一种状况是try 中的代码出现了异常,可是没有使用catch 进行异常捕获。但使用了finally 语句。像这种状况,出现异常的状况下,会先执行finally 中的代码,而后抛出异常。 代码以下:
List list3;
try {
print(list3[1]);
} finally {
print('执行Finally 中的代码');
}
// 输出结果:
/**
flutter: 执行Finally 中的代码
flutter: ══╡ EXCEPTION CAUGHT BY GESTURE ╞═══════════════════════════════════════════════════════════════════
flutter: The following NoSuchMethodError was thrown while handling a gesture:
flutter: The method '[]' was called on null.
flutter: Receiver: null
flutter: Tried calling: [](1)
flutter:
flutter: When the exception was thrown, this was the stack:
flutter: #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:50:5)
省略............
*/
复制代码
小编微信:可加并拉入《QiShare技术交流群》。
关注咱们的途径有:
QiShare(简书)
QiShare(掘金)
QiShare(知乎)
QiShare(GitHub)
QiShare(CocoaChina)
QiShare(StackOverflow)
QiShare(微信公众号)
推荐文章:
Dart基础(一)
Dart基础(二)
iOS 短信验证码倒计时按钮
iOS 环境变量配置
iOS 中处理定时任务的经常使用方法
算法小专栏:贪心算法
iOS 快速实现分页界面的搭建
iOS 中的界面旋转
奇舞周刊