Hello World
dart 版的hello world
main(List<String> args) {
print('Hello World');
}
和Java語言類似臭增,每個dart程序都有一個main,是整個程序的入口竹习。
將程序保存到hello_world.dart
文件中誊抛,執(zhí)行如下命令,就可以運行程序整陌。
dart hello_world.dart
變量定義
類似在JavaScript中一樣拗窃,你可以使用var關(guān)鍵字定義變量
main(List<String> args) {
var number = 42;
var name = 'Gurleen Sethi';
var salary = 150300.56;
var isDoorOpen = true;
}
但是瞎领,和JavaScript不同的是,在Dart2中随夸,一旦你給變量賦值一種類型的值九默,就不能再賦值另一種類型的值。Dart 可以自動從右邊數(shù)據(jù)推斷數(shù)據(jù)類型宾毒。
你也可以明確指定數(shù)據(jù)類型定義變量驼修。
main(List<String> args) {
int number = 42;
String name = 'Gurleen Sethi';
double salary = 150300.56;
bool isDoorOpen = true;
}
If you don’t intend to change the value held by a variable, then declare it with a final or a const.
如果你不想改變變量所持有的值,可以用關(guān)鍵字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變量只能被賦值一次妨退,他不需要在編譯時就賦值妇萄,可以在運行時賦值。
內(nèi)置的數(shù)據(jù)類型
dart語言提供所有現(xiàn)代語言提供的所有基本數(shù)據(jù)類型咬荷。
- Numbers
- Strings
- Booleans
- Lists
- Maps
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;
}
Lists
聲明一個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的內(nèi)容是不可改變的罕扎,可以使用關(guān)鍵字const
.
main(List<String> args) {
var list = const [1,2,3,4];
}
Maps
定義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構(gòu)造函數(shù)定義map杆查。
main(List<String> args) {
var squares = new Map();
squares[4] = 16;
}
如果你想定義編譯時常量的map,可以使用const
關(guān)鍵字。
main(List<String> args) {
var squares = const { //不能改變當(dāng)前map的值
2: 4,
3: 9,
4: 16,
5: 25
};
}
函數(shù)
dart中的函數(shù)和JavaScript中有點類似臀蛛。你需要定義就是函數(shù)的名字亲桦、返回值、參數(shù)浊仆。
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";
}
下面是定義一行函數(shù)的方法抡柿。
main(List<String> args) {
var name = fullName('John', 'Doe');
print(name);
}
fullName(String firstName, String lastName) => "$firstName $lastName";
命名參數(shù)
dart有個叫命名參數(shù)的東西舔琅。當(dāng)你調(diào)用函數(shù)的時候,你必須指定參數(shù)的名字沙绝。要使用命名參數(shù)搏明,可以將函數(shù)的參數(shù)包括在花括號{}內(nèi)鼠锈。
main(List<String> args) {
var name = fullName(firstName: 'John', lastName: 'Doe');
print(name);
}
fullName({String firstName, String lastName}) {
return "$firstName $lastName";
}
如果你在調(diào)用命名參數(shù)的函數(shù)時,沒有指定參數(shù)的名字星著,程序?qū)⒈罎ⅰ?/p>
參數(shù)默認值
你可以給函數(shù)的命名參數(shù)一個默認值购笆。下面的例子給lastName一個默認值。
main(List<String> args) {
var name = fullName(firstName: 'John');
print(name);
}
fullName({String firstName, String lastName = "Lazy"}) {
return "$firstName $lastName";
}
函數(shù)是一等公民
在dart中函數(shù)比較靈活虚循,例如同欠,你可以將函數(shù)當(dāng)參數(shù)傳遞給另一個函數(shù)。
main(List<String> args) {
out(printOutLoud);
}
out(void inner(String message)) {
inner('Message from inner function');
}
printOutLoud(String message) {
print(message.toUpperCase());
}
這里定義一個函數(shù)名字為out
,需要一個函數(shù)參數(shù)横缔。然后我定義一個名為printOutLoud
的函數(shù)铺遂,他所做的就是將字符串以大寫的形式打印。
dart 也有匿名函數(shù)茎刚,所以上面的例子中不用預(yù)定一個函數(shù)襟锐,而是傳遞一個匿名函數(shù)。
main(List<String> args) {
out((message) {
print(message.toUpperCase());
});
}
out(void inner(String message)) {
inner('Message from inner function');
}
另一個匿名函數(shù)的例子膛锭。
main(List<String> args) {
var list = [1,2,3,4];
list.forEach((item) {
print(item);
});
}
本教程結(jié)束粮坞。
參考
http://thetechnocafe.com/just-enough-dart-for-flutter-tutorial-01-variables-types-and-functions/