Skip to content

Instantly share code, notes, and snippets.

@krmao
Last active April 24, 2020 06:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krmao/5ad537a8e1fd0c4b8ba2ed7b6bf3b697 to your computer and use it in GitHub Desktop.
Save krmao/5ad537a8e1fd0c4b8ba2ed7b6bf3b697 to your computer and use it in GitHub Desktop.
learn dart language
import 'dart:io';
import 'dart:convert';
// Copyright 2015 the Dart project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file.
void main() {
// 1 var
print("\n变量----------------------\n");
/*
类似于JavaScript中的var,它可以接收任何类型的变量,但最大的不同是Dart中var变量一旦赋值,
类型便会确定,则不能再改变其类型
*/
var hello = "hello world";
// str = 1000; //类型一旦确定后则不能再更改其类型
print("$hello\n${hello + " " + hello}\n");
// 2 dynamic和Object
print("\n变量-dynamic和Object----------------------\n");
/*
Object 是Dart所有对象的根基类,也就是说所有类型都是Object的子类(包括 Function和Null),
所以任何类型的数据都可以赋值给Object声明的对象. dynamic与var一样都是关键词,
声明的变量可以赋值任意对象。
而dynamic与Object相同之处在于,他们声明的变量可以在后期改变赋值类型。
*/
dynamic t;
Object x;
t = "hi world";
x = 'Hello Object';
//下面代码没有问题
t = 1000;
x = 1000;
print("t=$t");
print("x=$x\n");
/*
dynamic与Object不同的是,dynamic声明的对象编译器会提供所有可能的组合,
而Object声明的对象只能使用Object的属性与方法, 否则编译器会报错。如:
*/
dynamic a;
Object b;
a = "";
b = "";
// no warning
print(a.length);
// warning:
// The getter 'length' is not defined for the class 'Object'
// print(b.length);
/*
变量a不会报错, 变量b编译器会报错
dynamic的这个特性与Objective-C中的id作用很像.
dynamic的这个特点使得我们在使用它时需要格外注意,这很容易引入一个运行时错误.
*/
//3 final和const
print("\n常量----------------------\n");
/*
* 如果您从未打算更改一个变量,那么使用 final 或 const,不是var,也不是一个类型。
* 一个 final 变量只能被设置一次,两者区别在于:
* const 变量是一个编译时常量,final变量在第一次使用时被初始化。
* 被final或者const修饰的变量,变量类型可以省略,如:
*/
//可以省略String这个类型声明
final str = "hi world";
//final String str = "hi world";
const str1 = "hi world";
//const String str1 = "hi world";
print("\nstr=$str");
print("str1=$str1\n");
// 4 循环
print("\n循环----------------------\n");
for (var i = 0; i < 4; i++) {
print('for -> $i');
}
// 5 函数
print("\n函数----------------------\n");
/*Dart是一种真正的面向对象的语言,
* 所以即使是函数也是对象,并且有一个类型Function。
* 这意味着函数可以赋值给变量或作为参数传递给其他函数,
* 这是函数式编程的典型特征。*/
bool isFlutter() {
return true;
}
print("isFlutter is bool =${isFlutter() is bool}");
print("isFlutter is dynamic =${isFlutter() is dynamic}");
// 5.1 Dart函数声明如果没有显式声明返回值类型时会
// 默认当做dynamic处理
isFlutter2() {
return true;
}
print("isFlutter2 is bool =${isFlutter2() is bool}");
print("isFlutter2 is dynamic =${isFlutter2() is dynamic}");
// 6 异步
print("\n异步----------------------\n");
Future.delayed(new Duration(seconds: 5), () {
print("异步 执行开始");
return "异步 hi world!";
throw AssertionError("Error");
}).then((data) {
//执行成功会走到这里
print("异步 执行成功");
print(data);
}).catchError((e) {
//执行失败会走到这里
print("异步 执行失败");
print(e);
}).whenComplete(() {
//无论成功或失败都会走到这里
print("异步 执行结束");
});
// 7 asyn wait
print("\nasyn wait----------------------\n");
callRequest();
}
///
/// async和await:
/// 这两个关键字的使用只需要记住两点:
/// 只有async方法才能使用await关键字调用方法
/// 如果调用别的async方法必须使用await关键字
/// async是让方法变成异步。
/// await是等待异步方法执行完成。
///
Future requestData() {
return Future.delayed(new Duration(seconds: 2), () {
return "hahahahaha!";
}).then((data) {
print(data);
}).catchError((e) {
print(e);
}).whenComplete(() {});
}
void callRequest() async {
print("callRequest start\n");
await requestData();
print("callRequest end\n\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment