基本样例,看起来跟其他语言没啥区别:
int add(int a, int b) {
    return a + b;
}
main() {
    for (int i = 0; i < 10; ++i) {
        print('idx:$i');
    }
    print('1 + 2 = ${add(1, 2)}');
}
强类型,末尾需要分号,看起来跟C语言很像。字符串里引用变量,跟es5比较像,但是不需要用斜引号。又有点像python,什么都可以print。
除了null,所有对象都继承自Object,甚至包括整数、函数。这点挺像python的。可以使用Object num = 100;
支持类型推导,所以可以使用var来声名类型;
以_打头的变量为私有变量,而不是通过public private protected,这点更像python,不像Java;
支持常规的C语言方式的三元判断:condition ? expr1 : expr2;
支持bool类型,值为true和false;
单引号和双引号没啥区别,三单引号、三双引号、r"",等等,跟python里是一模一样;
if判断的条件,结果必须为bool类型,if (List<int>[]) {} 是不行的,这点比较像golang;
因为bool判断的一些规定,所以判断数组是否为空,要写成:
if (a.length > 0) {
}
支持js里的三点符,所以可以写成:
List<int> a = [1, 2, 3];
var b = [1, 2, 3, ...a];
数组的索引不支持负数。遍历可以使用常规的for (var i = 0; i < a.length; ++i) {},还可以使用for (var i in a) {}。
奇怪的是,dart的List,末尾添加时使用add()方法,而其他语言一般叫push()。
在dart里,创建对象时,new关键字不是必须的。
有初始值就不需要写类型:
var s = {'abc', 'def'};
如果默认使用{},会被认为是Map,所以需要指定类型,或者给初始值,这点跟python一样。举例:
Set<String> s = {};
s.add('abc');
s.add('def');
if (s.contains('abc')) {
    print('exists');
} else {
    print('not exists');
}
默认{}就表示一个Map,跟python里的dict一样概念。举例:
Map<String, Object> m = {};
m['name'] = 'zhangsan';
m['age'] = 20;
print(m);
var mm = {
    'name': 'zhangsan',
    'age': 20,
};
print(mm);
一样也支持三点符,举例:
var mmm = {
    'pi': 3.14,
    ...mm
};
print(mmm);
有点类型于js里的箭头函数,其实是python里的lambda表达式,写法如下:
String toFixed(double f, int n) => f.toStringAsFixed(n);
print(toFixed(3.14159265, 2));
函数支持命名参数,命名参数都是可选的,举例:
//命名参数
String format({String name, int age}) {
    String res = '';
    if (name != null) {
        res += 'name:$name';
    }
    if (age != null) {
        res += 'age:$age';
    }
    return res;
}
可选的位置参数写法:
//位置参数
String format2(String name, [int age]) {
    String res = '';
    res += 'name:$name';
    if (age != null) {
        res += 'age:$age';
    }
    return res;
}
匿名函数比较容易跟js弄混,举例:
[1,2,3].forEach((i) {
    print(i*2);
});
定义一个对象:
class Point {
    double x = 0;
    double y = 0;
    double z = 0;
    //Point(double x, double y, double z) {
    //    this.x = x;
    //    this.y = y;
    //    this.z = z;
    //}
    Point(this.x, this.y, this.z);
}
例子中的构建函数,其语法糖展开之后,与上面注释的部分一致。
如下例子可以获取变量的类型:
var p = Point(1,2,3);
print('type: ${p.runtimeType}');
还支持初始化列表创建:
class Point {
    double x = 0;
    double y = 0;
    double z = 0;
    Point.fromJson(Map<String, double> json)
        : x = json['x'],
          y = json['y'],
          z = json['z'];
}
var p = Point.fromJson({'x': 1, 'y': 2, 'z': 3});
可以当函数使用的类:
import 'dart:math';
class Point {
    double x = 0;
    double y = 0;
    double z = 0;
    Point(this.x, this.y, this.z);
    double call(double x, double y, double z) {
        return sqrt(x*x + y*y + z*z);
    }
}
main() {
    var callable = Point(1,2,3);
    print(callable(1,2,3));
}
就像js一样,可以抛出任何类型的异常,但通常不建议这样,建议从Exception或者从Error继承。
int add(int a, int b) {
    if (a == 0 || b == 0) {
        throw Exception('exception occur');
    }
    return a + b;
}
main() {
    try {
        int r = add(0,2);
        print(r);
    } catch (e) {
        print(e);
    } finally {
        //other thing
    }
}
当然跟python一样,也可以catch不同的类型:
main() {
    try {
        int r = add(0,2);
        print(r);
    } on Exception catch (e) {
        print(e);
    } on Error catch (e) {
        print(e);
    }
}
跟其他语言类似的语法,只是async要放在后面,举例:
Future<int> add(int a, int b) async {
    return a + b;
}
main() async {
    var r = await add(1,2);
    print(r);
}