// 申明函数
void printHello() {
print('hello world');
}
// 返回类型为int
int getNum() {
return 123;
}
// 匿名函数 箭头函数
var myPrint = (value) => {
print(value1);
}
-----------------------------------------------------
void main() {
printHello();
print(getNum());
myPrint('x');
// 立执行函数
((int n){
print(n)
})(12);
}
void main() {
String userInfo(String name, [int age = 18], { int gender = 0 }) {
return 'name: $name, 年龄: $age';
}
String res = userInfo('max', 20, gender: 1);
print(res);
const myPrint = (res) => {
print(res)
}
List l = [1, 2, 3]
l.forEach(myPrint)
}
和js一样
import 'package:http/http.dart' as http; //pub.dev 类似npm
Future getdata() {
final url = 'https://httpbin.org/ip';
return http.get(url).then(res => {
print(res.body);
String ip = jsonDecode(res.body)['origin'];
return ip;
}).catch(err => {
print(err);
})
}
Future getdata() async {
final url = 'https://httpbin.org/ip';
final res = await http.get(url)
String ip = jsonDecode(res.body)['origin'];
return ip;
}
class Person {
String name = 'lys';
void getInfo() {
print('我是:$name')
}
}
void main() {
Persion p = new Persion(); //实例化
// 访问属性
print(p.name)
// 访问方法
p.getInfo()
// Dart 中所有内容都是对象
Map m = new Map();
print(m.length);
m.addAll({ 'name': 'aaa', 'age': 14 })
}
class Person {
String name;
static Person instance;
factory Person([String name = 'maxloong']) {
if(Person.instance == null) {
// 第一次实例化
Person.instance = new Person.newSelf(name);
}
return Person.instance;
}
// 命名构造函数
Person.newSelf(this.name);
}
class Rect {
int height;
int width;
Rect(): height = 2, width = 10 {
print(this.width, this.height);
}
getArea() {
return this.height * this.width;
}
}
abstract class Processor {
String cores;
arch(String name);
}
abstract class Camera {
String resolution;
brand(String name);
}
class Phone implements Processor, Camera {
String cores;
String resolution;
arch(String name) {
print(0, name)
}
brand(String name) {
print(1, name)
}
}
void main() {
}
泛型指的是 在函数,类,接口中指定宽泛数据类型的语法
通常,在尖括号中,使用一个字母来代表类型,例如E,T,S,K和V等
T getData<T>(T value) {
return value;
}
getData<int>(12);
getData<String>("12");
使用泛型减少重复代码
范型函数
T getData<T>(T value) {
return value;
}
// 普通
class CommonClass {
Set s = new Set<int>();
void add(int value) {
this.s.add(value);
}
void info() {
print(this.s);
}
}
void main() {
CommonClass c = new CommonClass();
c.add(1);
c.info();
GenericsClass g = new GenericsClass<String>();
g.add("1");
g.info();
}
// 泛型类
class
GenericsClass<T> {
Set s = new Set<T>();
void add(T value) {
this.s.add(value);
}
void info() {
print(this.s);
}
}
泛型接口
abstract class ObjCache {
getBykey(String key);
void setBykey(String key, Object value);
}
abstract class StringCache {
getBykey(String key);
void setBykey(String key, String value);
}
abstract class Cache<T> {
getBykey(String key);
void setBykey(String key, T value);
}
// 文件缓存
class FileCache<T> implements Cache<T> {
getBykey(String key) {
return null;
};
void setBykey(String key, T value) {
print("文件缓存");
};
}
// 内存缓存
class MemoryCache<T> implements Cache<T> {
getBykey(String key) {
return null;
};
void setBykey(String key, T value) {
print("文件缓存");
};
}
void main() {
FileCache fc = new FileCache<String>();
fc.setBykey('foo', 'bar');
FileCache fc = new FileCache<Map>();
fc.setBykey('index', {name: '张三丰'});
MemoryCache mc = new MemoryCache<String>();
mc.setBykey('foo', 'bar');
}
泛型类型限制
class SomeBaseClass {
// ...
}
class Foo<T extends SomeBaseClass> {
String toString() => "Instance of 'Foo<$T>'";
}
void main() {
var someBaseClassFoo = Foo<SomeBaseClass>();
print(someBaseClassFoo);
}