Dart学习笔记(二) | 字数总计: 2.4k | 阅读时长: 11分钟 | 阅读量:
Map 字典 官网文档
常⽤属性: keys
获取所有的key值values
获取所有的value值isEmpty
是否为空isNotEmpty
是否不为空常⽤⽅法: remove(key)
删除指定key的数据addAll({...})
合并映射 给映射内增加属性containsValue
查看映射内的值 返回true/falseforEach
map
where
any
every
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 var myMap = {};Map map = {};var map1 = new Map ();Map map2 = { 'test' :'test' }; print (map2['test' ]); Map testMap = {'a' : 1 , 'b' : 2 , 'c' : 3 };print (testMap.keys); print (testMap.values); print (testMap.isEmpty); print (testMap.isNotEmpty); testMap.remove('a' ); print (testMap); testMap.addAll({'d' :4 }); print (testMap); final planetsByMass = <num , String >{ 0.81 : 'Venus' , 1 : 'Earth' , 0.11 : 'Mars' , 17.15 : 'Neptune' }; planetsByMass.forEach((key, value) { print ('$key : $value ' ); }); final planetsByMass = <num , String >{ 0.81 : 'Venus' , 1 : 'Earth' , 0.11 : 'Mars' , 17.15 : 'Neptune' }; planetsByMass.forEach((key, value) { print ('$key : $value ' ); }); print (planetsByMass.map(((key, value) => MapEntry( key, value)))); print (planetsByMass.map(((key, value) => MapEntry( key + 1 , value))));
Set 官网文档 常用来数组去重
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 var arr = new Set (); arr.add('test1' ); arr.add('test1' ); print (arr.toList()); var arr1 = ['test' , 'test1' , 'test2' , 'test1' ]; var s = new Set (); s.addAll(arr1); print (s.toList()); var halogens = { 'fluorine' , 'chlorine' , 'bromine' , 'iodine' , 'astatine' }; var elements = <String >{}; elements.add('fluorine' ); print (elements); elements.addAll(halogens); print (elements);
箭头函数 箭头函数(和JS不⼀样,dart的箭头函数只能写⼀“⾏”)严谨的说法 1个表达式
1 2 var fruits = ['apple' , 'banana' , 'lemon' , 'grape' , 'orange' ];fruits.where((name) => name.contains('banana' )).forEach(print );
方法入参 参数 func(name)
强制类型 func(String name)
默认值 func(String name='name')
可选参数 func(name,[age,gender])
命名参数 func(name,{age,gender})
1 2 3 4 5 6 7 8 9 String say(String from, String msg, [String device = '' ]) { if (device.isNotEmpty) return '$from says $msg --- from $device ' ; return '$from says $msg ' ; } print (say('Jim' , 'Hi' )); print (say('Jim' , 'Hi' , 'iPhone' ));
1 2 3 4 5 6 7 String say(String from, String msg, {String software = '' , String device = '' }) { return '$from says $msg --- from $device $software ' ; } print (say('Jim' , 'Hi' , device: 'iPhone' , software: 'QQ' )); print (say('Jim' , 'Hi' , software: 'Wechat' ));
async,await,Future 多看看官网! 官网传送门
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 void foo() async { print ('foo E' ); String value = await bar(); print ('foo X $value ' ); } String bar() async { print ("bar E" ); return "hello" ; } main() { print ('main E' ); foo(); print ("main X" ); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 foo() { print ('foo E' ); return Future(bar).then((value) => print ('foo X $value ' )); } bar() async { print ("bar E" ); return "hello" ; } main() { print ('main E' ); foo(); print ("main X" ); }
dart的代码执行顺序和js的evenloop是类似的;
以下代码的执行顺序:
控制台打印start
; 执行countSeconds()方法,countSeconds中是Future.delayed,放入异步任务中; 执行printOrderMessage(), 控制台打印:Awaiting user order...
; 执行fetchUserOrder(), fetchUserOrder中是Future.delayed, 放入异步任务中; 这个时候先执行countSeconds中的异步任务,控制台依次打印1 2 3 4
; 再执行fetchUserOrder中的异步任务,得到order,控制台打印Your order is: Large Latte
; 控制台打印end
; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Future<void > printOrderMessage() async { print ('Awaiting user order...' ); var order = await fetchUserOrder(); print ('Your order is: $order ' ); } Future<String > fetchUserOrder() { return Future.delayed(const Duration (seconds: 4 ), () => 'Large Latte' ); } void main() async { print ('start' ); countSeconds(4 ); await printOrderMessage(); print ('end' ); } void countSeconds(int s) { for (var i = 1 ; i <= s; i++) { Future.delayed(Duration (seconds: i), () => print (i)); } }
打印结果:
1 2 3 4 5 6 7 8 start Awaiting user order... 1 2 3 4 Your order is: Large Latte end
以上的例子再稍稍做一下改动:将16行printOrderMessage
前面的await去掉,再来看看代码的打印顺序。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Future<void > printOrderMessage() async { print ('Awaiting user order...' ); var order = await fetchUserOrder(); print ('Your order is: $order ' ); } Future<String > fetchUserOrder() { return Future.delayed(const Duration (seconds: 4 ), () => 'Large Latte' ); } void main() async { print ('start' ); countSeconds(4 ); printOrderMessage(); print ('end' ); } void countSeconds(int s) { for (var i = 1 ; i <= s; i++) { Future.delayed(Duration (seconds: i), () => print (i)); } }
打印顺序:
1 2 3 4 5 6 7 8 9 start Awaiting user order... end 1 2 3 4 Your order is: Large Latte
Getter 和 Setter 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 class Rectangle { num left, top, width, height; Rectangle (this .left, this .top, this .width, this .height); num get right => left + width; set right (num value) => left = value - width; num get bottom => top + height; set bottom (num value) => top = value - height; } void main () { var rect = Rectangle (3 , 4 , 20 , 15 ); assert (rect.left == 3 ); rect.right = 12 ; assert (rect.left == -8 ); }
闭包 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Function makeAdder(num addBy) { return (num i) => addBy + i; } void main() { var add2 = makeAdder(2 ); var add4 = makeAdder(4 ); assert (add2(3 ) == 5 ); assert (add4(3 ) == 7 ); }
class 类 Dart 是一种基于类和 mixin 继承机制的面向对象的语言。每个对象都是一个类的实例,所有的类均继承自 Object。基于 Mixin 继承 意味着每个类都只有一个超类,一个类中的代码可以在其他多个继承类中重复使用。Dart 中的构造函数分为默认构造函数和命名构造函数。
默认构造函数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 class Person { Person(){ print ('this is person' ); } } class Student extends Person { } main(){ var stu = new Student(); }
命名构造函数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 class Student { final String firstName; final String lastName; final String name; Student(this .firstName,this .lastName) : name = firstName + lastName; } main(){ var stu = new Student('Halon' ,'Jim' ); print (stu.name); }
扩展类 继承 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 class Person { void doWork(){ print ('person work' ); } } class Student extends Person { @override void doWork(){ super .doWork(); print ('student study' ); } } main() { var student = Student(); student.doWork(); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 abstract class Vehicle { String get name; String get weapon; String get run; } class Fighter implements Vehicle { @override String get name => 'fighter' ; @override String get weapon => 'missile' ; @override String get run => 'fly' ; @override String toString() { return "$name can $run ,use $weapon as weapon." ; } } class Tank implements Vehicle { @override String get name => 'tank' ; @override String get weapon => 'cannon' ; @override String get run => 'run' ; @override String toString() { return "$name can $run ,use $weapon as weapon." ; } } Vehicle vehicleFactory(String type) { if (type == 'fighter' ) return Fighter(); if (type == 'tank' ) return Tank(); throw 'can not create $type .' ; } main() { final fighter = vehicleFactory('fighter' ); final tank = vehicleFactory('tank' ); print (fighter); print (tank); }
Mixin Mixin 可以为类添加额外的功能,而且使用 Mixin 与其他类之间也不存在层级和继承关系。使用 Mixin 也很简单,通过 with 后面跟一个或多个混入的名称就可以了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 abstract class Animal {}abstract class Mammal extends Animal {}abstract class Bird extends Animal {}abstract class Fish extends Animal {}mixin Walker { void walk() { print ("I'm walking" ); } } mixin Swimmer { void swim() { print ("I'm swimming" ); } } mixin Flyer { void Fly() { print ("I'm swimming" ); } } class Dolphin extends Mammal with Swimmer {}class Bat extends Mammal with Walker , Flyer {}class Cat extends Mammal with Walker {}class Dove extends Bird with Walker , Flyer {}class Duck extends Bird with Walker , Swimmer , Flyer {}class FlyingFish extends Fish with Swimmer , Flyer {}void main(List <String > args) { Cat cat = Cat(); Duck duck = Duck(); cat.walk(); duck.swim(); }