枚舉
- 枚舉是數(shù)量固定的常量值碍讨,通過
enum
關(guān)鍵字聲明
enum Color{red,green,blue}
- 枚舉的values常量治力,可以獲取所有枚舉值列表
List<Color>colors = Color.values;
- 可以通過
index
獲取值的索引
assert(Color.green.index==1);
enum Color { red, grren, blue }
void main() {
//通過index返回枚舉中具體常量的值
print(Color.grren.index);
//通過values 返回常量值列表
print(Color.values);
List<Color> colors = Color.values;
print(colors);
//通過下標(biāo),訪問列表中的內(nèi)容
print(colors[0]);
//通過forEach去遍歷列表的內(nèi)容
colors.forEach((element) {
print('valie:$element,index:${element.index}');
});
}
Dart庫與生態(tài)
自定義庫
// library MyCustom;
//建議寫成小寫字母+下劃線形式
library my_custom;
class MyCustom {
String name = 'MyCustom';
static num version = 1.0;
void info() {
print('我是自定義庫');
}
}
import 'lib/MyCustom.dart';
void main() {
MyCustom mc = new MyCustom();
mc.info();
print(MyCustom.version);
}
- 引入部分庫(僅引入需要的內(nèi)容)
- 包含引入(show)
- 排除引入(hide)
void f1() {
print('f1 is running');
}
void f2() {
print('f1 is running');
}
void f3() {
print('f1 is running');
}
import 'dart:math';
// import 'lib/common.dart' show f1, f3; //show后面指定包含引入的內(nèi)容
import 'lib/common.dart' hide f1, f3;//hide會隱藏后面的內(nèi)容
void main() {
f1();
//f2沒有在show里面指定
// f2();
f3();
}
void f1() {
print('f1 of function is running');
}
void hello() {
print('hello ');
}
import 'lib/common.dart';
import 'lib/function.dart' as func;//給庫添加前綴 解決命名沖突
void main() {
f1();
func.f1();
}
import 'lib/function.dart' deferred as func;
void main() {
//延遲加載
// func.f1();
// func.hello();
print(1);
greet();
print(2);
print(3);
}
Future greet() async {
await func.loadLibrary();
func.hello();
}
//與主庫建立聯(lián)系
part of phone;
class Cameraa {
String name = '攝像頭';
void info() {
print('我是攝像頭');
}
}
//與主庫建立聯(lián)系
part of phone;
class Processor {
String name = '處理器';
void info() {
print('我是處理器');
}
}
library phone;
//與分庫建立聯(lián)系
part 'Camera.dart';
part 'Processor.dart';
import '/phone/main.dart';
void main() {
Cameraa c = new Cameraa();
c.info();
Processor p = new Processor();
p.info();
}
import '29_part.dart' as phone ;
void main() {
phone.main();
}
第三方庫
創(chuàng)新無極限勃黍!只要敢想琴许,沒有什么不可能,立即跳出思維的框框吧溉躲。如果你正處于一個上升的朝陽行業(yè),那么嘗試去尋找更有效的解決方案:更招消費(fèi)者喜愛益兄、更簡潔的商業(yè)模式锻梳。如果你處于一個日漸萎縮的行業(yè),那么趕緊在自己變得跟不上時代之前抽身而出