基本類型生命以後都是null值 沒有默認java
int i = 1; //整數值
double d = 1.0; //double 64-bit (雙精度) 浮點數
double.maxFinite; //最大值
//int和double都是num的子類
num n1 = 1;
num n2 = 1.0;
//轉換
//String->int
int i2 = int.parse('1');
double d2 = 1; //當double的值爲int值時,int自動轉成double
//試着去分析 若是有誤返回null
int i2 = int.tryParse('1.0');//返回null
//int->String
String str = 123.toString(); //數字也是對象
複製代碼
var name = '123';
var names = '123 ${name}'; //拼接
var rawNames = r'123 ${name}'; //加r前綴 打印出來的結果是 '123 ${name}'
//三個單引號或者雙引號 能夠換行
var multiLinesString = ''' dongnao damon''';
複製代碼
//StringBuffer
var sb = StringBuffer(); //dart 2 能夠省略new
//..級聯符實現鏈式調用
sb..write('aaa')..write('bbb')..write('ccc');
sb.writeAll(['ddd', 'eee', 'fff'], ','); //第二個參數表示分隔符,將第一個參數列表裏的數據用這個分隔符拼接起來
複製代碼
bool isNull;
//自動長度
List growableList = List();
List growableList = new List()..length = 3;
//固定長度
var list = List(3); //List的聲明,能夠用var也可用List。
//元素類型固定
var typeList = List<int>();
複製代碼
int first = typeList.first; //第一個
int last = typeList.last; //最後一個
int length = typeList.length; //長度
bool isEmpty = typeList.isEmpty; //是空
bool isNotEmpty = typeList.isNotEmpty;// 不是空
Iterable reversed = typeList.reversed; //反轉
複製代碼
var list4 = [];
// 添加 插入
list4.add(1);
list4.addAll([2, 3, 4]);
list4.insert(0, 0);
list4.insertAll(1, [5, 6, 7]);
//刪
list4.remove(5);
list4.removeAt(2);
//改
list4[4] = 5;
//range 從0開始 連續的三個數會被修改爲9 批量修改
list4.fillRange(0, 3, 9);
//獲取水印區間的值
Iterable getRange = list4.getRange(0, 3);
//查
//是否包含
var contains = list4.contains(5);
//獲取value的索引
var indexOf = list4.indexOf(1);
//條件去查找value的索引
int indexWhere = list4.indexWhere((test) => test == 5);
//排序
list4.sort();
//洗牌
list4.shuffle();
//複製子列表 截取列表中一段
var list5 = list4.sublist(1);
//操做符 集合相加
var list6 = [8, 9];
var list7 = list5 + list6;
複製代碼
Set無重複列表
var dynamicSet = Set();
var set1 = {'123', 'asd'};
var set2 = {'123', 'qwe', 'asd'};
//set1 裏獨有的元素
var difference12 = set1.difference(set2);
//set2中獨有的袁術
var difference21 = set2.difference(set1);
//交集
var intersection = set1.intersection(set2);
//並集
var union = set1.union(set2);
/只保留目標集合到當前set中
set2.retainAll(['123', 'asfd']);
複製代碼
//forEach
collection.forEach((item) => print('forEach: $item'));
//for-in遍歷元素
for (var item in collection) {
print('for-in: $item');
}
複製代碼
var dynamicMap = Map();
var map = Map<int, String>();
var map1 = {'name': 'dongnao', 1: 'android'};
map1.addAll({'name':'damon'});
複製代碼
//經常使用屬性
(map.isEmpty); //是否爲空
(map.isNotEmpty); //是否不爲空
(map.length); //鍵值對個數
(map.keys); //key 集合
(map.values); //value集合
複製代碼
//Runes用於在字符串中表示Unicode字符 https://copychar.cc/emoji/
String runesStr = '👄';
Runes runes = new Runes('\u{1f605} \u6211');
var str1 = String.fromCharCodes(runes); //使用String.fromCharCodes顯示字符圖形
String str2 = '\u{1f605} \u6211'; //若是非4個數值,須要把編碼值放到大括號中
複製代碼
ymbol標識符 主要是反射用,如今mirrors已經被移除了
全部的函數都返回一個值。若是沒有指定返回值,則 默認把語句 return null; 做爲函數的最後一個語句執行。android
main() {
//普通函數定義
int add(int x, int y) {
return x + y;
}
testFunc() {}
//可省略參數類型(不建議)
add1(x, y) {
return x + y;
}
//箭頭函數:=>表達式
int add2(int x, int y) => x + y;
//可選命名參數:使用 {param1, param2, …} 的形式來指定命名參數
int add3({int x, int y, int z}) {
x ??= 1;
y ??= 2;
z ??= 3;
return x + y + z;
}
//可選位置參數:把可選參數放到 [] 中,必填參數要放在可選參數前面
int add4(int x, [int y, int z]) {
y ??= 2;
z ??= 3;
return x + y + z;
}
//可選命名參數默認值(默認值必須是編譯時常量),目前能夠使用等號'='或冒號':'
int add5(int x, {int y = 2, int z = 3}) {
return x + y + z;
}
//可選位置參數默認值(默認值必須是編譯時常量),只能使用等號'='
int add6(int x, [int y = 2, int z = 3]) {
return x + y + z;
}
//使用list或map做爲默認值,但必須是const
void func(
{List list = const [1, 2, 3],
Map map = const {1: 1, 'name': 'dongnao'}}) {
//TODO ...
}
//無參匿名函數
var anonFunc1 = () => print('無參匿名函數');
//有參匿名函數
var anonFunc = (name) => 'I am $name';
//匿名函數傳參
List test(List list, String func(str)) {
for (var i = 0; i < list.length; i++) {
list[i] = func(list[i]);
}
return list;
}
var list = ['d', 'a', 'm', 'o', 'n'];
test(list, (str) => str * 2)
//List.forEach()就用的匿名函數
List list1 = [11, 12, 13];
list1.forEach((item) => print('$item'));
//返回Function對象(閉包)
Function makeAddFunc(int x) {
x++;
return (int y) => x + y;
}
var addFunc = makeAddFunc(2);
print(addFunc(3));
// 函數別名
MyFunc myFunc;
//能夠指向任何同簽名的函數
myFunc = subtsract;
myFunc(4, 2);
myFunc = divide;
myFunc(4, 2);
//typedef 做爲參數傳遞給函數
calculator(4, 2, subtsract);
}
//函數別名
typedef MyFunc(int a, int b);
//根據MyFunc相同的函數簽名定義兩個函數
subtsract(int a, int b) {
print('subtsract: ${a - b}');
}
divide(int a, int b) {
print('divide: ${a / b}');
}
//typedef 也能夠做爲參數傳遞給函數
calculator(int a, int b, MyFunc func) {
func(a, b);
}
複製代碼
?. 判空的bash
as、is、is! 強轉 , 是這個類型返回true, 是這個類型返回false閉包
A/B=D,A ~/ B = C D是帶精度的數字 C是個整數 就是商ide
三目運算符 A ? A : B 或者A ?? B函數
.. 級聯操做符 返回使用的當前對象ui
//拋出Exception對象
throw new FormatException('格式異常');
//拋出Error對象
throw new NullThrownError();
//拋出任意非null對象
throw '這是一個異常';
/// ---------------------------------異常的捕獲try catch--------------------------------
try {
throw new OutOfMemoryError();
} on OutOfMemoryError {
//on 指定異常類型
// rethrow; //把捕獲的異常給 從新拋出
} on Error {
//捕獲Error類型
} on Exception catch (e) {
//捕獲Exception類型
} catch (e, s) {
//catch() 能夠帶有一個或者兩個參數, 第一個參數爲拋出的異常對象, 第二個爲StackTrace對象堆棧信息
}
複製代碼