变量
基本类型和String 示例:
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 import 'package:flutter/material.dart' ;void main() => runApp(new MyApp());class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo' , home: Scaffold( appBar: AppBar( title: Text('基本数据类型' ), ), body: Center( child: RaisedButton( onPressed: _initData, child: Text('roll' ) ), ), ), ); } void _initData(){ bool done = true ; int num = 2 ; double x = 3.14 ; final bool visible = false ; final int amount = 100 ; final double y = 2.7 ; const bool debug = true ; const int sum = 42 ; const double z = 1.2 ; debugPrint('$done ' ); debugPrint('$num ' ); debugPrint('$x ' ); debugPrint('$visible ' ); debugPrint('$amount ' ); debugPrint('$y ' ); debugPrint('$debug ' ); debugPrint('$sum ' ); debugPrint('$z ' ); var str = ' str' ; var str2 = str.toUpperCase(); var str3 = str.trim(); debugPrint(str); debugPrint(str2); debugPrint(str3); var b1 = identical(str, str2); var b2 = (str == str2); debugPrint('$b1 ' ); debugPrint('$b2 ' ); assert (str == str2); assert (!identical(str, str2)); } }
集合 List 示例:
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 void _initData(){ var list = List <int >(); list.add(1 ); list.add(2 ); debugPrint('list:' +list.toString()); var list2 = [1 , 2 ]; debugPrint('list2:' +list2.toString()); var list3 =<int >[]; list3.add(1 ); list3.add(2 ); debugPrint('list3:' +list3.toString()); var list4 = const [1 , 2 ]; list4 = [4 , 5 ]; debugPrint('list4:' +list4.toString()); const list5 = [1 , 2 ]; var list6 = [1 , 3 , 5 , 7 ]; for (var e in list6){ debugPrint('list6:$e ' ); } }
Set 示例:
1 2 3 4 5 6 7 8 9 void _initData(){ var set = Set <String >(); set .add('foo' ); set .add('bar' ); assert (set .contains('foo' )); }
Map 示例:
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 void _initData(){ var map = Map <String ,int >(); map['foo' ] = 1 ; map['bar' ] = 3 ; debugPrint('添加:' +map.toString()); map['foo' ] = 4 ; debugPrint('修改:' +map.toString()); if (map['foobar' ] == null ) { debugPrint('map does not contain foobar' ); } var map2 = const { 'foo' : 2 , 'bar' : 4 , }; debugPrint('map2:' +map2.toString()); var map3 = <String ,String >{}; debugPrint('map3:' +map3.toString()); }
dynamic 和 Object 示例:
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 void _initData(){ Object o = 'string' ; o = 66 ; debugPrint(o.toString()); dynamic obj = 'string' ; obj['foo' ] = 6 ; dynamic obj1 = <String ,int >{}; if (obj1 is Map <String ,int >){ obj1['foo' ] = 66 ; debugPrint(obj1.toString()); } var map = obj1 as Map <String ,int >; debugPrint(map.toString()); }
语句
示例:
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 void _initData(){ var success = true ; if (success){ print ('done' ); }else { print ('fail' ); } for (var i = 0 ;i < 5 ;i++){ print ('for:' ); print (i); } var sum = 0 ; var j = 1 ; do { sum += j; ++j; print ('do / while:' ); print (sum); print (j); }while (j < 5 ); while (sum-- > 0 ){ print ('while:' ); print (sum); } var type = 1 ; switch (type){ case 0 : break ; case 1 : break ; case 2 : break ; default : break ; } }
函数
示例:
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 import 'package:flutter/material.dart' ;void main() => runApp(new MyApp());class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo' , home: Scaffold( appBar: AppBar( title: Text('函数' ), ), body: Center( child: RaisedButton( onPressed: _initData, child: Text('roll' ) ), ), ), ); } void _initData(){ print ('支持可选参数' ); print (calculate(1 )); print (calculate(1 ,2 )); print ('支持默认参数' ); print (calculate2(1 )); print ('支持具名参数' ); print (calculate3(x:1 ,y:2 )); print (calculate3(y:3 ,x:4 )); print ('函数还可以在函数的内部定义' ); var adder = makeAdder(2 ); print (adder(1 , 2 )); } int calculate(int x,[int y]){ if (y != null ){ return x + y ; } return x ; } int calculate2(int x,[int y = 1 ]){ return x + y ; } int calculate3({int x, int y = 0 }) { return x + y; } int calculate4({@required int x, @required int y}) { return x + y; } } typedef Adder = int Function (int ,int );Adder makeAdder(int extra){ int adder(int x, int y){ return x + y + extra; } return adder; }
异常
抛出异常 1 2 3 4 5 void _initData(){ throw Exception('the error' ); }
捕获异常 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 void _initData() { try { } on FormatException catch (e) { debugPrint('FormatException:$e ' ); } on Exception catch (e) { debugPrint('Exception:$e ' ); }catch (e){ debugPrint('catch:$e ' ); }finally { print ('finally' ); } }
类
Dart 提供的更简洁的 Class 初始化方式:
1 2 3 4 5 6 class MyClass { int x; int y; MyClass(this .x,this .y); }
还可以使用初始化列表(initializer list)对对象进行初始化:
1 2 3 4 5 6 7 8 9 10 11 class MyClass { int x; int y; MyClass(int x,int y):x = x,y = y{ } }
Dart 具有垃圾收集功能,对象的使用跟 Java 里几乎是一样的:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 void main(){ var point = MyClass(1 , 2 ); point.x = 4 ; print (point); } class MyClass { int x; int y; MyClass(this .x,this .y); @override String toString() { return "MyClass{x=$x , y=$y }" ; } }
Dart 使用 package 的概念来管理源码和可见性。 它没有 public、private 之类的访问权限控制符,默认情况下,所有的符号都是公开的。 如果不想某个变量对包的外部可见,可以使用下划线开头来给变量命名。
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 main() { var point = _OffsetPoint(1 ,2 ,10 ); print (point.getX); print (point); point.setX = 4 ; print (point); } class _OffsetPoint { int _x; int _y; int offset; _OffsetPoint(int x,int y, int offset): _x = x, _y = y, offset = offset {} int get getX => _x + offset; int get getY { return _y + offset; } void set setX (int x) =>_x=x; void set setY(int y)=>_y=y; @override String toString() { return "OffsetPoint{x=$getX , y=$getY }" ; } }
类的继承
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 70 void main(){ var log = Point3D(1 ,2 ,3 ); print (log); var p=Point3D2(1 ,2 ,3 ); p.bark(); } class Point2D { int x; int y; Point2D(int x, int y){ this .x = x; this .y = y; print ("Ponit2D(构造函数):$x ----$y " ); this .x = 5 ; } } class Point3D extends Point2D { int z; Point3D(int x,int y,int z):z = z,super (x,y){ print ("Point3D(构造函数):$x ----$y ----$z " ); } @override String toString() { return "Point3D(toString):$x ----$y ----$z " ; } } abstract class Bark { void bark() { print ('woof' ); }} class Point3D2 extends Point2D with Bark { int z; Point3D2(int x, int y, int z):z=z,super (x,y){} } class Foo extends Bark {}
泛型
示例:
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 class Pair <S ,T > { S first; T second; Pair(this .first,this .second); } void main(){ var p = Pair('hello' ,2 ); print (p is Pair<String ,int >); print (p is ! Pair<int ,int >); print (p is Pair); }
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 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 import 'dart:io' ;void main(){ foo(); foo2(); foo2(); foo(); } void foo(){ var file=File('path-to-your-file' ); file.exists() .then((exists)=>print ('foo: file ${exists?'exists' :'not exists' } ' )) .catchError((e)=>print (e)); print ('foo: after file.exists() returned' ); } void foo2() async { var file=File('path-to-your-file' ); try { var exists = await file.exists(); print ('foo2: file ${exists ? 'exists' : 'not exists' } ' ); print ('foo2: after file.exists() returned' ); }catch (e){ print (e); } }
备注
参考资料 :Dart英文官方文档 Flutter学习指南:熟悉Dart语言