一.Hello World
1.主函數
void main(List<String> args){
}
2.導庫
import
①標準庫 import “dart:io”
②自定義 import “路徑”
③Pub import “package:路徑”
④show;hide;as;library;export
二.變量類型和數據類型
1.關鍵字:const/final/var
2.數據類型:object/dynamic/int/double/boolen/string
①const和final要在定義時賦值,不可改,const編譯時,final運行時
②dynamic可跨數據類型賦值,其他不可
③真假值判斷時必須爲true或false,不可用其他類型代替
④’’/""/’’’ ‘’’
⑤$變量名 ${變量名} ${變量名.runtimeType}
⑥dynamic比object靈活
三.集合類型
1.List/Set/Map
①List < dynamic > args;Set < dynamic > args;Set < dynamic, dynamic > args
②List自動排序可重複,Set無序不可重複
③List.from(Set.from())
④for dynamic arg in List< dynamic > args
⑤打印List.foreach((item)=> print(item)) (匿名函數)
四.函數
1.箭頭函數
2.必傳參數 / 命名可選參數{} / 位置可選參數[]
①add(num1,num2) => nun1+num2
②默認參數必須爲可選參數
③匿名函數
④詞法作用域,向外查找
⑤閉包
main(List<String> args) {
add(num a) {
return (num b) {
return b + a;
};
}
var sum = add(2);
print(add(2)); //4
print(add(6)); //8
⑥參數可爲函數
五.運算符
1.??=
2.??
3.級聯運算符(…)
六.流程控制
七.類
1.構造函數
①語法糖
②命名構造函數(不支持函數的重載的解決辦法)
class Person{
String name;
int age;
Person(this.name,this.age); //語法糖
Person.fromMap(Map<String,int> map){
this.name=map["name"];
this.age=map["age"];
} //Person person=Person.fromMap(Map<String,int> map);
}
③初始化列表
④重定向構造函數
class Rectangle{
double width;
double height;
double area;
Rectangle(this.width,this.height):this.area=this.width*this.height;
Rectangle.inputWidth(double width):this(width,0);
}
⑤常量構造函數
class Person {
final String name;
const Person(this.name);
}
Person perone=const Person("A");
Person pertwo=const Person("B");
identical(perone,pertwo) //True
⑥工廠構造函數
⑦setter和getter
2.繼承
①extends
②override
③子類中賦值父類中的屬性
class Animal{
int age;
Animal(this.age);
}
class Dog extends Animal{
string name;
Dog(this name,int age):super(age);
}
3.抽象類
①不能實例化
②定義抽象函數,繼承的類必須重載覆寫函數
abstract class Animal{
void eat();
}
4.隱式接口
①implements
②所有方法必須重新實現
5.Mixin
mixin Runner{
void running(){
print("running");
}
}
mixin Swimer{
void swimming(){
print("swimming");
}
}
class Person with Runner,Swimer{
}
5.泛型
class Location<T>{
T x;
T y;
Location(this.x,this.y);
}
Location lone=Location<int>(1,2);
Location ltwo=Location<double>(1.1,2.2);
T popFirst(List<T> list){
return list[0]
}
popFirst(List<int> list);
popFirst(List<double> list);