var
,它能夠接收任何類型的變量,但最大的不一樣是Dart中var變量一旦賦值,類型便會肯定,則不能再改變其類型,如:var t;
t="yc";
// 下面代碼在dart中會報錯,由於變量t的類型已經肯定爲String,
// 類型一旦肯定後則不能再更改其類型。
t=1000;
複製代碼
var curPage = 0;
var title = "瀟湘劍雨:小楊逗比";
複製代碼
//final 表示常量
final title = "瀟湘劍雨:小楊逗比";
複製代碼
//static const 組合表明了靜態常量
static const String complete = "COMPLETE";
複製代碼
const
變量是一個編譯時常量,final
變量在第一次使用時被初始化。被final
或者const
修飾的變量,而且變量類型能夠省略。//定義初始化一個變量
final double number = 13.14;
number = 520;
//調用打印數字方法
printNumber(number);
複製代碼
//在Java中,下面操做可行
ToastUtils.showRoundRectToast("二維碼掃描"+1000);
//在dart中,下面錯誤操做,編譯不經過,直接會有紅色提示
int yc = 0;
print("瀟湘劍雨" + yc);
//在dart中,下面正確操做
int yc = 0;
print("瀟湘劍雨" + yc.toString());
複製代碼
Object
是dart全部對象的根基類,也就是說全部類型都是Object
的子類(包括Function和Null),因此任何類型的數據均可以賦值給Object
聲明的對象.
dynamic
與var
同樣都是關鍵詞,聲明的變量能夠賦值任意對象.而dynamic
與Object
相同之處在於,他們聲明的變量能夠在後期改變賦值類型.dynamic t;
Object x;
t = "hi world";
x = 'Hello Object';
//下面代碼沒有問題
t = 1000;
x = 1000;
複製代碼
dynamic
與Object
不一樣的是,dynamic
聲明的對象編譯器會提供全部可能的組合,而Object
聲明的對象只能使用Object的屬性與方法, 不然編譯器會報錯. 如:dynamic a;
Object b;
main() {
a = "";
b = "";
printLengths();
}
printLengths() {
// no warning
print(a.length);
// warning:
// The getter 'length' is not defined for the class 'Object'
print(b.length);
}
複製代碼
dynamic
的這個特性與Objective-C
中的id
做用很像.dynamic
的這個特色使得咱們在使用它是須要格外注意,這很容易引入一個運行時錯誤.//int類型 這裏沒有long類型
var positionIndex = 0;
//double類型 這裏沒有float類型
var time = 1993.03;
複製代碼
//定義初始化一個變量
double number = 13.14;
複製代碼
//相似Java中的布爾類型
bool mIsLogin = false;
if (!mIsLogin) {
//沒有登錄
print('沒有登錄');
} else {
//已經登錄
Navigator.of(context).push(new MaterialPageRoute(builder: (context) {
return new CollectPage();
}));
}
複製代碼
String name ="yc";
//報錯 由於name不是bool類型
if(name){
print(name);
}
複製代碼
// 檢查是否爲空字符串
var fullName = 'doubi';
assert(fullName.isEmpty);
// 檢查是否小於等於0
var hitPoints = 0;
assert(hitPoints <= 0);
// 檢查是否爲 null.
var unicorn;
assert(unicorn == null);
// 檢查是否爲 NaN.
var iMeantToDoThis = 0 / 0;
assert(iMeantToDoThis.isNaN);
複製代碼
//字符串
var title = "瀟湘劍雨:小楊逗比";
複製代碼
main(List<String> args) {
//或者
List arr1 = [1,2,3,4];
var arr2 = [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);
}
複製代碼
var list = const [1,2,3,4];
複製代碼
void test() {
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 檢查是否存在
var containsKey = map.containsKey('value1');
print(containsKey);
var entries = map.entries;
var values = map.values;
}
複製代碼
2019-06-20 17:22:39.200 4281-4329/com.hwmc.auth I/flutter: value1
2019-06-20 17:22:39.200 4281-4329/com.hwmc.auth I/flutter: null
2019-06-20 17:22:39.200 4281-4329/com.hwmc.auth I/flutter: 4
2019-06-20 17:22:39.200 4281-4329/com.hwmc.auth I/flutter: false
複製代碼
var squares = new Map();
squares["a"] = 1;
squares["b"] = 2;
squares["c"] = 3.0;
squares["d"] = [1,2];
squares["e"] = "yc逗比";
print(squares['a']);
print(squares['e']);
複製代碼
2019-06-20 17:27:32.841 4281-4329/com.hwmc.auth I/flutter: 1
2019-06-20 17:27:32.841 4281-4329/com.hwmc.auth I/flutter: yc逗比
複製代碼
void test(){
var name = fullName('楊充', '逗比');
print(name);
}
String fullName(String firstName, String lastName) {
return "$firstName $lastName";
}
複製代碼
void test(){
var name = fullName('楊充', '逗比');
print(name);
}
String fullName(String firstName, String lastName) {
return "$firstName $lastName";
}
複製代碼
void test(){
var name = fullName('楊充', '逗比');
print(name);
}
fullName(String firstName, String lastName) {
return "$firstName $lastName";
}
複製代碼
void test(){
out(printOutLoud);
}
out(void inner(String message)) {
inner('Message from inner function');
}
printOutLoud(String message) {
print(message.toUpperCase());
}
複製代碼
複製代碼
var number = 57;
if (number > 100) {
print('Large Number');
} else if (number < 100) {
print('Small Number');
} else {
print('Number is 100');
}
複製代碼
int age = 60;
String status = age < 50 ? "年輕人" : "老年人";
複製代碼
void test() {
for (int i = 0; i < 10; i++) {
print('$i');
}
}
複製代碼
void test() {
int i = 0;
while(i < 10) {
print('$i');
i++;
}
}
複製代碼
void test() {
int i = 0;
do {
print('$i');
i++;
} while (i < 10);
}
複製代碼
void test() {
int age = 50;
switch(age) {
case 10:
print('Too Young.');
break;
case 20:
case 30:
print('Still Young!');
break;
case 40:
print('Getting old.');
break;
case 50:
print('You are old!');
break;
}
}
複製代碼
void testA() async{
new Future<String>(() {
return "This is a doubi";
});
}
Future testB() async{
return new Future<String>(() {
return "This is a doubi";
});
}
Future<String> testC() {
return new Future<String>(() {
return "This is a doubi";
});
}
複製代碼
void test() {
getTest().then((value) {
print("測試----------"+value);
}).catchError((error) {
print('測試----------Error');
});
}
Future<String> getTest() {
return new Future<String>(() {
return "This is a doubi";
});
}
//打印結果
2019-06-21 17:11:12.941 16501-16583/com.hwmc.auth I/flutter: 測試----------This is a doubi
複製代碼
void test() {
getTest().then((value) {
print("測試----------"+value);
}).catchError((error) {
print('測試----------Error');
});
}
Future<String> getTest() {
return new Future<String>(() {
return "This is a doubi";
});
}
//打印結果
2019-06-21 17:18:46.896 16501-16583/com.hwmc.auth I/flutter: 測試----------Error
複製代碼
void test() {
getTest().then((value) {
print("測試----------"+value);
}).catchError((error) {
print('測試----------Error');
});
}
Future<String> getTest() {
return new Future<String>.delayed(new Duration(milliseconds: 2000),() {
return "This is a doubi";
});
}
複製代碼
void test() {
getTest().then((value) {
print("測試----------"+value);
}).catchError((error) {
print('測試----------Error');
});
print('測試----------逗比是這個先執行嗎');
}
Future<String> getTest() {
return new Future<String>.delayed(new Duration(milliseconds: 2000),() {
return "This is a doubi";
});
}
2019-06-21 17:26:16.619 16501-16583/com.hwmc.auth I/flutter: 測試----------逗比是這個先執行嗎
2019-06-21 17:26:17.176 16501-16583/com.hwmc.auth I/flutter: 測試----------This is a doubi
複製代碼
void test1(){
Dog d = new Dog();
}
class Dog {
}
var cat = new Cat("逗比", 12);
class Cat {
String name;
int age;
Cat(String name, int age) {
this.name = name;
this.age = age;
}
}
複製代碼
var cat = new Cat("逗比", 12);
class Cat {
String name;
int age;
Cat(String name, int age) {
this.name = name;
this.age = age;
}
}
複製代碼
Map map = new Map();
map['name']= "哈巴狗";
map['age'] = 5;
Dog d = new Dog.newBorn(map);
class Dog {
String name;
int age;
Dog(this.name, this.age);
Dog.newBorn(Map json) {
name = json['name'];
age = json['age'];
}
}
複製代碼
Pug p = new Pug('哈哈哈', 5);
print(p.name);
class Dog {
String name;
int age;
Dog(this.name, this.age);
Dog.newBorn() {
name = 'Doggy';
age = 0;
}
}
class Pug extends Dog {
Pug(String name, int age): super(name, age);
}
複製代碼
Pug p = new Pug.small('哈哈哈');
print(p.name);
class Dog {
String name;
int age;
Dog(this.name, this.age);
Dog.newBorn() {
name = '逗比';
age = 0;
}
}
class Pug extends Dog {
Pug(String name, int age): super(name, age);
Pug.small(String name): this(name, 1);
Pug.large(String name): this(name, 3);
}
複製代碼
Pug p = new Pug();
print(p.bark());
class Dog {
bark() {
print('Bow Wow');
}
}
class Pug extends Dog {
@override
bark() {
print('你真是個逗比!');
}
}
複製代碼
複製代碼
abstract class AbstractDog {
void eat();
void _hiddenMethod();
}
class SmallDog extends AbstractDog{
@override
void _hiddenMethod() {
}
@override
void eat() {
}
}
複製代碼
void test() {
Dog d = new Dog('哈巴狗', 5);
//這個報錯
print(d.name);
print(d.age);
}
複製代碼
class Dog {
String _name;
int age;
Dog(this._name, this.age);
String get respectedName {
return 'Mr.$_name';
}
set respectedName(String newName) {
_name = newName;
}
Dog.newBorn() {
_name = '哈巴狗';
age = 0;
}
bark() {
print('Bow Wow');
}
_hiddenMethod() {
print('I can only be called internally!');
}
}
複製代碼
void test() {
Dog.bark();
}
class Dog {
static bark() {
print('Bow Wow');
}
}
複製代碼
DataHolder<String> dataHolder = new DataHolder('Some data');
print(dataHolder.getData());
dataHolder.setData('New Data');
print(dataHolder.getData());
//下面這個會報錯,由於dataHolder對象在建立的時候就已經限制爲String類型
dataHolder.setData(123);
print(dataHolder.getData());
class DataHolder<T> {
T data;
DataHolder(this.data);
getData() {
return data;
}
setData(data) {
this.data = data;
}
}
複製代碼
Future test() async {
try {
String value = await getTest();
print("測試----------"+value);
} catch(e) {
print('測試----------Error');
}
print('測試----------逗比是這個先執行嗎');
}
Future<String> getTest() {
return new Future<String>.delayed(new Duration(milliseconds: 2000),() {
return "This is a doubi";
});
}
2019-06-21 17:32:37.701 16501-16583/com.hwmc.auth I/flutter: 測試----------This is a doubi
2019-06-21 17:32:37.702 16501-16583/com.hwmc.auth I/flutter: 測試----------逗比是這個先執行嗎
複製代碼
void getHttp async {
// TODO ---
}
複製代碼
String loadAppVersion() => "1.0.2"
複製代碼
Futre<String> loadAppVersion() async => "1.0.2"
複製代碼
void main() {
//調用異步方法
doAsync();
}
// 在函數上聲明瞭 async 代表這是一個異步方法
Future<bool> doAsync() async {
try {
// 這裏是一個模擬請求一個網絡耗時操做
var result = await getHttp();
//請求出來的結果
return printResult(result);
} catch (e) {
print(e);
return false;
}
}
//將請求出來的結果打印出來
Future<bool> printResult(summary) {
print(summary);
}
//開始模擬網絡請求 等待 5 秒返回一個字符串
getHttp() {
return new Future.delayed(Duration(seconds: 5), () => "Request Succeeded");
}
複製代碼
void main() {
doAsync();
}
Future<String> doAsync() async {
return getHttp().then((r){
return printResult(r);
}).catchError((e){
print(e);
});
}
Future<String> printResult(summary) {
print(summary);
}
Future<String> getHttp() {
return new Future.delayed(Duration(seconds: 5), () => "Request Succeeded");
}
複製代碼
void test1(){
divide(10, 0);
}
divide(int a, int b) {
if (b == 0) {
throw new IntegerDivisionByZeroException();
}
return a / b;
}
複製代碼
void test1(){
divide(10, 0);
}
divide(int a, int b) {
if (b == 0) {
throw new Exception('逗比,不能爲0的');
}
return a / b;
}
複製代碼
void test1(){
try {
divide(10, 0);
} on IntegerDivisionByZeroException {
print('逗比,異常被捕獲了');
}
}
divide(int a, int b) {
if (b == 0) {
throw new IntegerDivisionByZeroException();
}
return a / b;
}
複製代碼
void test1(){
try {
divide(10, 0);
} on IntegerDivisionByZeroException {
print('逗比,異常被捕獲了');
}
}
divide(int a, int b) {
if (b == 0) {
throw new Exception('逗比,不能爲0的');
}
return a / b;
}
複製代碼
void test1(){
try {
divide(10, 0);
} on IntegerDivisionByZeroException {
print('逗比,異常被捕獲了');
} catch (e) {
print(e);
}
}
divide(int a, int b) {
if (b == 0) {
throw new Exception('yc other exception.');
}
return a / b;
}
複製代碼
void test1(){
try {
divide(10, 0);
} on IntegerDivisionByZeroException {
print('逗比,異常被捕獲了');
} catch (e) {
print(e);
}finally {
print('I will always be executed!');
}
}
divide(int a, int b) {
if (b == 0) {
throw new Exception('yc other exception.');
}
return a / b;
}
複製代碼
Dog d = new Dog('哈巴狗', 12, CurrentState.sleeping);
print(d.state == CurrentState.sleeping); //Prints 'true'
enum CurrentState {
sleeping,
barking,
eating,
walking
}
class Dog {
String name;
int age;
CurrentState state;
Dog(this.name, this.age, this.state);
static bark() {
print('Bow Wow');
}
}
複製代碼
複製代碼
void test1() {
doSomething();
}
@toDo('seth', 'make this do something')
void doSomething() {
print('do something');
}
class toDo {
final String who;
final String what;
const toDo(this.who, this.what);
}
複製代碼
String str1 = '單引號字符串';
String str2 = "雙引號字符串";
print(str1); //輸出:單引號字符串
print(str2); //輸出:雙引號字符串
複製代碼
String str1 = '單引號中的"雙引號"字符串';
String str2 = "雙引號中的'單引號'字符串";
print("yc-str1--" + str1);
print("yc-str2--" + str2);
//單引號裏面有單引號,必須在前面加反斜槓
String str3 = '單引號中的\'單引號\'';
String str4 = "雙引號裏面有雙引號,\"雙引號\"";
print("yc-str3--" + str3);
print("yc-str4--" + str4);
複製代碼
2019-06-21 17:52:07.722 16501-16583/com.hwmc.auth I/flutter: yc-str1--單引號中的"雙引號"字符串
2019-06-21 17:52:07.723 16501-16583/com.hwmc.auth I/flutter: yc-str2--雙引號中的'單引號'字符串
2019-06-21 17:52:07.723 16501-16583/com.hwmc.auth I/flutter: yc-str3--單引號中的'單引號'
2019-06-21 17:52:07.723 16501-16583/com.hwmc.auth I/flutter: yc-str4--雙引號裏面有雙引號,"雙引號"
複製代碼
//String str5 = '單引號''''單引號'; //報錯了,逗比
String str6 = '單引號'' ''單引號';
String str7 = '單引號''*''單引號';
String str8 = "雙引號"" ""雙引號";
String str9 = "雙引號""*""雙引號";
//String str10 = "雙引號""""雙引號"; //報錯了,逗比
print("yc-str6--" + str6);
print("yc-str7--" + str7);
print("yc-str8--" + str8);
print("yc-str9--" + str9);
複製代碼
2019-06-21 17:56:21.847 16501-16583/com.hwmc.auth I/flutter: yc-str6--單引號 單引號
2019-06-21 17:56:21.847 16501-16583/com.hwmc.auth I/flutter: yc-str7--單引號*單引號
2019-06-21 17:56:21.847 16501-16583/com.hwmc.auth I/flutter: yc-str8--雙引號 雙引號
2019-06-21 17:56:21.847 16501-16583/com.hwmc.auth I/flutter: yc-str9--雙引號*雙引號
複製代碼