Dart4Flutter -01 – 變量, 類型和 函數bash
Dart4Flutter -拾遺01 - flutter-dart環境搭建ui
flutter入門 - 狀態管理this
Flutter 入門-本地訪問-MethodChannelspa
dart 版的hello world3d
main(List<String> args) {
print('Hello World');
}
複製代碼
和Java語言相似,每一個dart程序都有一個main,是整個程序的入口。code
將程序保存到hello_world.dart
文件中,執行以下命令,就能夠運行程序。
dart hello_world.dart
相似在JavaScript中同樣,你可使用var關鍵字定義變量
main(List<String> args) {
var number = 42;
var name = 'Gurleen Sethi';
var salary = 150300.56;
var isDoorOpen = true;
}
複製代碼
可是,和JavaScript不一樣的是,在Dart2中,一旦你給變量賦值一種類型的值,就不能再賦值另外一種類型的值。Dart 能夠自動從右邊數據推斷數據類型。
你也能夠明確指定數據類型定義變量。
main(List<String> args) {
int number = 42;
String name = 'Gurleen Sethi';
double salary = 150300.56;
bool isDoorOpen = true;
}
複製代碼
若是你不想改變變量所持有的值,能夠用關鍵字final
或者const
聲明。
main(List<String> args) {
final int number = 42;
const String name = 'Gurleen Sethi';
//Omit explicitly defining data types
final salary = 150300.56;
const isDoorOpen = true;
}
複製代碼
final 和 const的不一樣在於,const是編譯時常量。例如,const變量在編譯時必需要有一個值。例如,const PI = 3.14
,然而final變量只能被賦值一次,他不須要在編譯時就賦值,能夠在運行時賦值。
dart語言提供全部現代語言提供的全部基本數據類型。
main(List<String> args) {
//Numbers
int x = 100;
double y = 1.1;
int z = int.parse('10');
double d = double.parse('44.4');
//Strings
String s = 'This is a string';
String backslash = 'I can\'t speak';
//String interpolation
String interpolated = 'Value of x is $x'; //Prints: Value of x is 100
String interpolated2 = 'Value of s is ${s.toLowerCase()}'; //Prints: Value of s is this is a string
//Booleans
bool isDoorOpen = false;
}
複製代碼
聲明一個list很是的簡單,能夠簡單使用方括號[]定義list。下面是list的經常使用操做。
main(List<String> args) {
var list = [1,2,3,4];
print(list); //Output: [1, 2, 3, 4]
//Length 長度
print(list.length);
//Selecting single value 獲取單個值
print(list[1]); //Outout: 2
//Adding a value 添加值到list
list.add(10);
//Removing a single isntance of value 刪除單個值
list.remove(3);
//Remove at a particular position 刪除指定位置的值
list.removeAt(0);
}
複製代碼
若是你想定義一個編譯時常量list,例如,list的內容是不可改變的,可使用關鍵字const
.
main(List<String> args) {
var list = const [1,2,3,4];
}
複製代碼
定義map也很簡單。可使用花括號{}定義map。
main(List<String> args) {
var map = {
'key1': 'value1',
'key2': 'value2',
'key3': 'value3'
};
//Fetching the values 獲取值
print(map['key1']); //Output: value1
print(map['test']); //Output: null
//Add a new value 添加值
map['key4'] = 'value4';
//Length 獲取長度
print(map.length);
//Check if a key is present 檢查是否存在
map.containsKey('value1');
//Get entries and values
var entries = map.entries;
var values = map.values;
}
複製代碼
你也可使用map構造函數定義map。
main(List<String> args) {
var squares = new Map();
squares[4] = 16;
}
複製代碼
若是你想定義編譯時常量的map,可使用const
關鍵字。
main(List<String> args) {
var squares = const { //不能改變當前map的值
2: 4,
3: 9,
4: 16,
5: 25
};
}
複製代碼
dart中的函數和JavaScript中有點相似。你須要定義就是函數的名字、返回值、參數。
main(List<String> args) {
var name = fullName('John', 'Doe');
print(name);
}
String fullName(String firstName, String lastName) {
return "$firstName $lastName";
}
複製代碼
你也能夠省略返回值類型,程序一樣能夠運行。
main(List<String> args) {
var name = fullName('John', 'Doe');
print(name);
}
fullName(String firstName, String lastName) {
return "$firstName $lastName";
}
複製代碼
下面是定義一行函數的方法。
main(List<String> args) {
var name = fullName('John', 'Doe');
print(name);
}
fullName(String firstName, String lastName) => "$firstName $lastName";
複製代碼
dart有個叫命名參數的東西。當你調用函數的時候,你必須指定參數的名字。要使用命名參數,能夠將函數的參數包括在花括號{}內。
main(List<String> args) {
var name = fullName(firstName: 'John', lastName: 'Doe');
print(name);
}
fullName({String firstName, String lastName}) {
return "$firstName $lastName";
}
複製代碼
若是你在調用命名參數的函數時,沒有指定參數的名字,程序將崩潰。
你能夠給函數的命名參數一個默認值。下面的例子給lastName一個默認值。
main(List<String> args) {
var name = fullName(firstName: 'John');
print(name);
}
fullName({String firstName, String lastName = "Lazy"}) {
return "$firstName $lastName";
}
複製代碼
在dart中函數比較靈活,例如,你能夠將函數當參數傳遞給另外一個函數。
main(List<String> args) {
out(printOutLoud);
}
out(void inner(String message)) {
inner('Message from inner function');
}
printOutLoud(String message) {
print(message.toUpperCase());
}
複製代碼
這裏定義一個函數名字爲out
,須要一個函數參數。而後我定義一個名爲printOutLoud
的函數,他所作的就是將字符串以大寫的形式打印。
dart 也有匿名函數,因此上面的例子中不用預約一個函數,而是傳遞一個匿名函數。
main(List<String> args) {
out((message) {
print(message.toUpperCase());
});
}
out(void inner(String message)) {
inner('Message from inner function');
}
複製代碼
另外一個匿名函數的例子。
main(List<String> args) {
var list = [1,2,3,4];
list.forEach((item) {
print(item);
});
}
複製代碼
本教程結束。
下一講 dart4flutter – 02 –控制流 和異常
參考