赞
踩
这是一篇总结dart入门文章中对一些觉得惊艳且实用的语法记录,初学,有的地方理解不对,勿喷。
_变量名 一下划线开始的变量名表示私有的
1:string
多行书写一行显示:当字符串太长时可以使用如下方式,这种书写方式最终显示还会是一行。
var s1 = 'String ' 'concatenation' " works even over line breaks.";
多行书写多行显示,这种方式验证遵守换行,将显示
var s1 = ''' You can create multi-line strings like this one. ''';
显示 \n \t等 ,如下将显示一行,去掉r将显示两行
var s = r"In a raw string, even \n isn't special.";
字符串中填入变量常量,hello的值为 hello world
const user = "hello"; const hello = '$user world';
2:map
如下等同于 new map(),dart2.0中
Note: You might expect to see new Map()
instead of just Map()
. As of Dart 2, the new
keyword is optional(可选择的). For details, see Using constructors.
var gifts = { // Key: Value 'first': 'partridge', 'second': 'turtledoves', 'fifth': 'golden rings' };
gifts['fourth'] = 'calling birds';
3:参数
可选参数 {}或者[]中的参数为可选参数
void enableFlags({bool bold, bool hidden}) { // ... }
String say(String from, String msg, [String device = 'carrier pigeon', String mood]) { var result = '$from says $msg'; if (device != null) { result = '$result with a $device'; } if (mood != null) { result = '$result (in a $mood mood)'; } return result; }
默认参数
/// Sets the [bold] and [hidden] flags ... void enableFlags({bool bold = false, bool hidden = false}) { // ... }
如上{}[]作为可选参数的区别为,{}调用时需要加上别名,而[]调用时是按照顺序的,具体如下:
{}作为可选的使用:
enableFlags(bold: true);[]作为可选的使用:
say('Bob', 'Howdy','我是可选1,'我是可选2')
方法作为参数:
void printElement(int element) { print(element); } var list = [1, 2, 3]; // Pass printElement as a parameter. list.forEach(printElement);
4:操作符
~/ 这种操作符返回的是一个int数
5 ~/ 2 //2 Result is an int
??= 左边的数如果为null,就右边的结果赋值给它
var b = null; // 之后b的值为10 b ??= 10;
?? 如果左边为null就用右边的值,用法如下,如果name变量为null,那么函数返回Guest,否者返回name的值
String playerName(String name) => name ?? 'Guest';
?. 如果一个对象部位null,才执行.之后的语句,无论是函数还是属性.
.. 不好说,相当于builder模式,返回调用之前的对象,很强大
var test =new Map(); test[1]="hello1"; test[2]="hello2"; test[3]="hello3"; //以上等同于 var test1= new Map()..[1]="hello1"..[2]="hello2"..[3]="hello3";
5:switch
dart中switch没有break会报错,如下:
var command = 'OPEN'; switch (command) { case 'OPEN': executeOpen(); // ERROR: Missing break case 'CLOSED': executeClosed(); break; }
但有时逻辑有需要穿透下去,使用方式如下:
var command = 'CLOSED'; switch (command) { case 'CLOSED': // Empty case falls through. case 'NOW_CLOSED': // Runs for both CLOSED and NOW_CLOSED. executeNowClosed(); break; }
如有CLOSED分支也要逻辑,使用方式如下:
var command = 'CLOSED'; switch (command) { case 'CLOSED': executeClosed(); continue nowClosed; // Continues executing at the nowClosed label. nowClosed: case 'NOW_CLOSED': // Runs for both CLOSED and NOW_CLOSED. executeNowClosed(); break; }
6:异常
使用方式如下:
try { breedMoreLlamas(); } on OutOfLlamasException { // A specific exception buyMoreLlamas(); } on Exception catch (e) { // Anything else that is an exception print('Unknown exception: $e'); } catch (e) {
// catch中可以有两个参数catch(e,s) e:异常信息 s:堆栈信息 print('Something really unknown: $e');rethrow; // rethrow可以重新抛出这个异常,
}
7:import
show从一个package中只导入某一个类,hide 除了某个类都导入
// Import only foo. import 'package:lib1/lib1.dart' show foo; // Import all names EXCEPT foo. import 'package:lib2/lib2.dart' hide foo;
延迟导入,使用时才导入
import 'package:greetings/hello.dart' deferred as hello;
When you need the library, invoke(调用) loadLibrary()
using the library’s identifier.
Future greet() async { await hello.loadLibrary(); hello.printGreeting(); }
8:可调用类
代码如下,可以让一个类的实例调用像方法一样调用。
class WannabeFunction {
call(String a, String b, String c) => '$a $b $c!';
}
main() {
var wf = new WannabeFunction();
var out = wf("Hi","there,","gang");
print('$out');
}
9:typedef
可以给一些方法 类 取别名
typedef Compare = int Function(Object a, Object b);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。