Dart根底
1.变量与常量
变量
- var
- Object :对象
- dynamic :
常量
- final: 运行时分配内存
- const: 编译时分配内存
2.数据类型
- int 整型
- double 浮点型
- String 字符串
- List 数组
- Map 对象
3.函数
- 返回值类型:没有则是void
- 办法名
- 参数 :默认参数{};可选参数[]
//返回值类型 办法名 默认参数{} 应用:test1(1,str:'ccc')String test1(int id,{String str=""}){ Map map={1:'aaa',2:'bbb'}; return map[id] + str;}//可选参数[] 应用: test2(2,'ddd')String test2(int id,[String str]){ Map map={1:'aaa',2:'bbb'}; return map[id] + (str != null ? str :'');}// 匿名函数List list = [1,2,3];list.forEach((element) { print(element);});
4.类与继承
- 类:class
- 继承:extends
void main() { var p = new Person('su',18); p.say(); var p2 = new Student('su',18,'一年级'); p2.say();}//类class Person{ String name; int age; Person(String name,int age){ this.name = name; this.age = age; } void say(){ print(this.name+'=='+this.age.toString()); }}//继承class Student extends Person{ String className; Student(String name, int age,String className) : super(name, age){ this.className = className; } //重写父类say @override void say(){ super.say();//调用父类say办法 print(this.name+'=='+this.age.toString()+'=='+this.className); }}
5.mixin 与 抽象类
- 继承多个类 :with
- 抽象类:abstract 只定义方法,不实现
void main() { var p = new Person('s',18); p.eat(); p.sleep(); p.say();}class Eat{ void eat(){ print('eat'); } void say(){ print('bbb'); }}class Sleep{ void sleep(){ print('sleep'); } void say(){ print('aaa'); }}// with 继承两个类class Person extends Student with Eat,Sleep{ String name; int age; Person(name,age){ this.name =name; this.age =age; } void say(){ print(this.name+'=='+this.age.toString()); } //继承抽象类student,须要实现类的办法 @override void study() { print('study'); }}//抽象类, 只定义方法,不实现abstract class Student{ void study();}
6.应用库
- 本地援用本人编写的 import 'package/test.dart';
援用库 :import 'package:http/http.dart' as http;
- 地址:https://pub.dev/ ;
- 批改pubspec.yaml配置文件
- 援用零碎自带库:import 'dart:math';
提早加载库:deferred as ;
- 期待加载完后在应用 await math.loadLibrary();
- 导入库的一部分:show hide
7.异步解决
- async await
- Future
void main() async { Future.wait([ Future.delayed(new Duration(seconds: 1),(){ print(1); }), Future.delayed(new Duration(seconds: 2),(){ print(2); }) ]).then((value) { print(value); }); await Future.delayed(new Duration(seconds: 1),(){ print(1111); }); print(222);}