線上編譯器
Hello World!
dart Hello.dart
1// Hello.dart
2void main() {
3 print('Hello World!');
4}
Comment
1// Inline Comment
2
3/*
4Comment
5*/
Data Type
int
,double
,num
String
bool
List
Map
Set
Number
1void main() {
2 var num1 = 1;
3 print(num1); // 1
4
5 var num2 = 2.05;
6 print(num2); // 2.05
7
8 var num3 = int.parse('1');
9 print(num3); // 1
10
11 var num4 = double.parse('2.05');
12 print(num4); // 2.05
13
14 var sum = num3 + num4;
15 print('Sum = ${sum}'); // Sum = 3.05
16}
Constant
1void main() {
2 const one = 1;
3 const PI = 3.14;
4 const product = one * PI;
5 print(product); // 3.14
6}
String
1void main() {
2 var s1 = 'Hello';
3 var s2 = 'World';
4 var s3 = s1 + ' ' + s2;
5 print(s3); // Hello World
6
7 String one = 1.toString();
8 print(one); // 1
9
10 String PI = 3.14159.toStringAsFixed(2);
11 print(PI); // 3.14
12}
Boolean
1void main() {
2 String s1 = "Hello";
3 String s2 = "World";
4 bool val = (s1 == s2);
5 print(val); // false
6}
List
1void main() {
2 var L = [1, 2, 3];
3 L[0] = 10;
4 print(L); // [10, 2, 3]
5
6 var constL = const [1, 2, 3];
7
8 var L2 = [1, ...L]; // [1, 10, 2, 3];
9 print(L2);
10}
Set
1void main() {
2 var set1 = {'Monkdy', 'Dog', 'Goat'};
3 Set<String> empty_set1 = {};
4 var set2 = <String>{};
5 set2.add('Dog');
6 set2.addAll(set1);
7 print(set2.length); // 3
8}
Map
1void main() {
2 var M1 = {
3 'first': 1,
4 'second': 2,
5 'third': 3,
6 };
7 assert(M1['fourth'] == null);
8 print(M1['third']); // 3
9
10 Map<int, String> M2 = {};
11 M2[0] = '0';
12 M2[1999] = '1999';
13}
dynamic
, final
, const
- 沒有被宣告類型的變數會被宣告為
dynamic
。 final
和const
都不可以修改,const
用於編譯時期常數。final(const) dataType name
1void main() {
2 dynamic x = 10;
3 x = 20;
4 final y = x;
5 const z = 10;
6}
位元運算子
&
|
~
^
if
, else
1void main() {
2 var x = -1;
3 if (x == 0) {
4 print('x = 0');
5 } else if (x > 0) {
6 print('x > 0');
7 } else {
8 print('x < 0');
9 }
10}
switch
1void main() {
2 var ch = 'a';
3 switch(ch) {
4 case 'a': {
5 print('Apple');
6 }
7 break;
8 case 'b': {
9 print('Banana');
10 }
11 break;
12 default: {
13 print('Other');
14 }
15 break;
16 }
17}
for
1void main() {
2 var list = [2, 3, 5, 7];
3 for (var i in list) {
4 print(i);
5 }
6}
7/*
82
93
105
117
12*/
do
, while
1void main() {
2 var i = 1;
3 do {
4 print(i);
5 } while (i < 1);
6}
7/*
81
9*/
Function
1void main() {
2 print(test('hello ', 'world')); // hello world
3}
4String test(String s1, String s2) {
5 return s1 + s2;
6}
1void main() {
2 test('a', 'b');
3}
4void test(s1, [s2, s3]) {
5 print(s1);
6 print(s2);
7 print(s3);
8}
9/*
10a
11b
12null
13*/
- 預設引數。
1void main() {
2 test('a', s3: 'b');
3}
4void test(s1, {s2, s3: 'c'}) {
5 print(s1);
6 print(s2);
7 print(s3);
8}
9/*
10a
11null
12b
13*/
class
- 格式
1class class_name {
2 <fields>
3 <getters/setters>
4 <constructors>
5 <functions>
6}
- 繼承和
override
1void main() {
2 Child c = new Child();
3 c.m(); // child
4}
5
6class Parent {
7 void m() {
8 print('parent');
9 }
10}
11
12class Child extends Parent {
13 @override
14 void m() {
15 print('child');
16 }
17}
this
,super
static
class_name.constructor_name(param_list)
..
1void main() {
2 new Parent()
3 ..m1()
4 ..m2(1);
5}
6
7class Parent {
8 void m1() {
9 print('parent');
10 }
11 void m2(int i) {
12 print(i);
13 }
14}