Dart入門

陳小嬰
9 min readSep 4, 2018

--

寫Flutter之前先弄懂Dart

關鍵字(Keywords)

1 為系統內置識別字,2為Dart 1.0之後使用的識別字

常數(Constant)

  1. final
final String language = "Dart";final name = "Bond";

2.const (編譯常量compile-time constants)

和final的差異 → const在編譯時就載入,final在所屬的class初始化時才載入

//定義常數
const num = 15;
//產生常數指定給變數
var foo = const [];
final bar = const [];
const baz = const [];

變數(Variable)

var number = 1;
var message = 'text';

沒有初始值時,預設為null。

型別

  1. number:num, int , double

型別轉換example:

// String -> int
var one = int.parse('1');
// String -> double
var onePointOne = double.parse('1.1');
// int -> String
String oneAsString = 1.toString();
// double -> String
String piAsString = 3.14159.toStringAsFixed(2);

常用操作:

a. 運算符+-*/ , ~/取整數, %取餘數

b. isNaN是否非數字,iSEven是否為偶數, isOdd是否為奇數

2.String:UTF-16

var s1 = 'Single quotes work well for string literals.'; //單引號var s2 = "Double quotes work just as well."; //雙引號var s3 = 'Dart has $s1 and $s2';var s4 = 'That deserves all caps. ' + '${s1.toUpperCase()} is very handy!';  //插值表達式var s4_text = 'That deserves all caps. ' + 'STRING INTERPOLATION is very handy!';String s5 = '''Hello,
Dart.'''; //三引號,多行字符串
String s6 = "Hello,\n Dart."; //換行符var r1 = s1 * 5; //重複n次var r2 = (s1 == s2); //判斷是否相等var r3 = s1[1]; //取字元var r4 = s1.length; //長度var 45 = s1.isEmpty; //是否為空

3.boolean

bool a = true;bool b = false;

4.list (array)

var list1 = new List();var list2 = [1,2,3,"Dart",true]; //元素可以是不同型別list[0]  = "Hello";var list3 = const [1,2,3]; //不可變的listlist2.add("Hi");  //從後方添加list2.insert(1,"iOS"); //從index添加list1.clear(); //清空list2.sublist(1); //子陣列list2.forEach(print); //遍歷list.asMap(); //list轉map

5.map

var searchTerms = new Map();var nobleGases = new Map<int, String>();var language = {"first":"dart","second":"swift",1:true};print(language["first"]);print(language.keys);print(language.values);language.forEach(function1);
void function1(key,value) {
print("key=$key,value=$value");
}

6.dynamic (泛型)

var a;
a = 10;
a = "dart";
dynamic b = 20;var list = new List<dynamic>();

7.runes (for expressing Unicode characters in a string)

8.symbol

運算符(operator)

1.算術運算符

int a = 10;
int b = 20;
print(a ~/ b); //取整數
print(a % b); //取餘數
print(a++);
print(++a);

2.關係運算符

int a = 10;
int b = 20;
print(a == b);
print(a != b);
print(a > b);
print(a <= b);
String c = "123";
String d = "321";
print(c == d); //判斷字符串相等

3.邏輯運算符

bool a = true;
bool b = false;
print(!a);
print(a && b);
print(a || b);

4.賦值運算符

int a = 10;
int b;
b ??= 10; //如果變數沒有值,才給值a += 2;
a -= b;
a ~/= b;
a %= b;

5.條件表達式、三元語法糖

int genter = 0;
String str = (gender == 0 ? "M" : "F");
String a;
String b = "Dart";
String c = a ?? b; //a為空時就使用b

控制流程

1.if else 判斷

int score = 90;if (score > 90) {
print(""優秀);
} else (score => 60) {
print("良好");
} else {
print("不及格");
}

2.for迴圈

var list = [1,2,3,4];for (var index = 0,index < list.length;index ++) {
print(list[index]);
}
for (var item in list) {
print(item);
}

3.while迴圈

var count = 0;while(count < 5) {
print(count++);
}
do {
print(count--);
} while (count > 0 && count < 5);

4.break和continue

var list = [1,2,3,4,5];for (var item, in list) {
if(item == 2) {
continue; //繼續下一個迴圈
} if (item == 4) {
break; //終止所在迴圈
}
print(item);
}

5.switch case

String language = "Dart";switch(language) {
case A:
print("a");
break;
case "Dart":
print("language is Dart");
break;
case "Swift":
print("language is Swift");
continue A; //跳轉到自定義標記
case "Java":
print("language is Java");
break;
default:
break;
}

函式(Function)

1.定義函式

void main() { 
func1([1,2,3]);
}
String func1(List args) {
print(args);
return "123";
}
printName(name) {
print("name = $name"); //回傳值和參數類型都可以省略
//沒有回傳值時,默認為null
}
//箭頭省略僅適用一個表達式
var result = getName(name) => return "name = $name";

2.可選參數

void main() { 
printPerson("Dart");
printPerson("Dart", age: 10);
printPerson2("Dart");
printPerson2("Dart",10);
}
printPerson(String name, {int age, String gender}) {
// age和gender為可選參數,可以不傳
}
printPerson2(String name ,[int age, String gender]) {
//age和gender為可選參數,不使用變數名稱,依照順序傳值
//可選參數需定義於固定參數後面
}

3.默認參數值

void main() { 
printPerson("Dart");
printPerson("Dart", age: 10, gender: "M");
}
printPerson(String name, {int age = 5, String gender = "F"}) {
}

4.方法對象

void main() { 
Function func = printName; //方法對象可以是變數或參數
var list = [1,2,3];
listTimes(list, times);
}
void printName() {
print("Name");
}
List listTimes(List list,String times(str)) {
for(var index = 0;index< list.length;index++) {
list[index] = times(list[index]);
}
return list;
}
String times(str) {
return str*3;
}

5.匿名方法

void main() { 
var func = (name) {
print("hello,$name");
}
func("Dart");
var list = [1,2,3];
var result = listTimes(list,(name) { return name * 3 });
}
List listTimes(List list,String times(str)) {
for(var index = 0;index< list.length;index++) {
list[index] = times(list[index]);
}
return list;
}

6.閉包

void main() { 
var func = a();
func(); //0
func(); //1
}
a() {
int count = 0; //持有局部變量的狀態
return () {
print(count++) ;
};
}

參考:

Dart

Dart 语法基础

Dart快速入門

Flutter开发第一步-Dart编程语言入门

--

--

陳小嬰
陳小嬰

Written by 陳小嬰

喜愛動物又注重環保的iOS工程師就是我。Write the code change the world.

No responses yet