1 数据类型
-
数字(number) int double
- 字符串转换成 num int.parse(“1”) double.parse(“1”);
- double 四舍五入保留两位小数 toStringAsFixed(2) 返回值为string
- double 直接舍弃小数点后几位的数据 可使用字符串截取的方式
-
字符串(string)
- 单引号 双引号 三引号
- 三引号 可以输出文本格式 比如换行
-
布尔值(boolean)
-
列表(list)
- 一旦定义了 内容的类型 就不可变
- list内容是常量 添加const
-
集合(set)
- 映射(map)
- 符文(rune)
- 符号(Symbol)
- 数字(number) int double
- 字符串转换成 num int.parse("1") double.parse("1");
- double 四舍五入保留两位小数 toStringAsFixed(2) 返回值为string
- double 直接舍弃小数点后几位的数据 可使用字符串截取的方式
//字符串转换成 num
var a = int.parse("1");
var b = double.parse("1");
print(a);
print(b);
//结果
//1
//1.0
- 字符串(string)
- 单引号 双引号 三引号
- 三引号 可以输出文本格式 比如换行
var a = "aaa"
"bbb";
print(a);
a = 'aaa'
'bbb';
print(a);
a = '''aaa
bbb''';
print(a);
结果
aaabbb
aaabbb
aaa
bbb
- 列表(list)
- 一旦定义了 内容的类型 就不可变
- - list内容是常量 添加const
void main(){
var list = [1,2,3];
// list = ["1","2","3"]; // Error: A value of type 'String' can't be assigned to a variable of type 'int'.
var cars = [ Car("TSLT"),Car("XRB")];
print(cars);
print(cars[0].name);
print(cars[0].drive());
var names = const ["TSLT","XRBS"]; //内容不可改变
print(names);
names[1] = "TSLC"; // Cannot modify an unmodifiable list
print(names);
}
class Car{
var name;
Car(this.name);
bool drive(){
print("${name}我着了...");
return true;
}
}
结果
[Instance of 'Car', Instance of 'Car']
TSLT
TSLT我着了...
true
[TSLT, XRBS]
void main(){
var a = {1,"2",3.4,Car(5),true};
print(a);
a.add("aaa");
print(a);
var b = Set();
b.add(1);
b.add("2");
b.add(3.4);
b.add(Car(5));
b.add(true);
print(b);
// var c = const {1,"2",3.4,Car(5),true}; // Error: The element '3.4' does not have a primitive equality.
var c = const {1,"你好",true}; // Error: The element '3.4' does not have a primitive equality.
print(c);
}
结果
{1, 2, 3.4, Instance of 'Car', true}
{1, 2, 3.4, Instance of 'Car', true, aaa}
{1, 2, 3.4, Instance of 'Car', true}
{1, 你好, true}
2 var 关键字
- var 关键字一旦赋值类型即确定
void main(){
var a = 1;
var b = "hello";
var c = Car();
c.drive();
print(a.runtimeType);
print(b.runtimeType);
print(c.runtimeType);
}
class Car{
void drive(){
print("我跑了");
}
}
结果
我跑了
int
String
Car
3 dynamic 和 Object
- Object是Dart所有对象的父类
- Object和dynamic声明变量 可改变类型
- dynamic 可使用对象类型的属性和方法 Object声明的对象只能用Object的属性和方法
void main(){
Object a;
a = "hello";
print("a 的类型是 ${a.runtimeType}");
a = 1;
print("a 的变化后类型是 ${a.runtimeType}");
dynamic b;
b = 1;
print("b 的类型是 ${b.runtimeType}");
b = 1.1;
print("b 的变化后类型是 ${b.runtimeType}");
a = "hello";
b = "world";
print(b.length);
// print(a.length); //报错 The getter 'length' isn't defined for the class 'Object'.
}
结果
a 的类型是 String
a 的变化后类型是 int
b 的类型是 int
b 的变化后类型是 double
5
4 final 和 const
- const 编译时赋值
- final 运行时 第一次使用时 赋值
void main(){
final a;
a = 1;
print(a);
// const b; // 报错 Error: The const variable 'b' must be initialized
const b = 2;
print(b);
}
结果
1
2
5 空安全
void main(){
Car? car;
print(car?.drive());
car = Car();
print(car?.drive());
}
class Car{
bool drive(){
print("我着了...");
return true;
}
}
运行结果
lib/demo.dart:8:9: Warning: Operand of null-aware operation '?.' has type 'Car' which excludes null.
- 'Car' is from 'package:demo/demo.dart' ('lib/demo.dart').
print(car?.drive());
^
null
我着了...
true
函数 (方法)
- 不声明返回值的 默认是dynamic
run(){
print("我跑了");
}
eat(){
return "吃撑了";
}
void main(){
print(run());
print(eat());
}
结果
我跑了
null
吃撑了
- 函数可以作为变量
var run = (){
print("我跑了");
};
var eat = (){
return "吃撑了";
};
void main(){
print(run());
print(eat());
}
结果
我跑了
null
吃撑了
- 函数可以作为参数
run(String name){
print("${name}跑了");
}
eat(String name){
return "${name}吃撑了";
}
see(var seeWho){
seeWho("老六");
}
void main(){
var car = Car("安妮");
car.who(run);
see((var name)=>print(name));
}
class Car{
var name;
Car(this.name);
who(var run){
run(name);
}
}
运行结果:
安妮跑了
老六
- 可变函数
eat(String name,{String? food,int? num}){
return "${name}吃${food}撑了${num}份";
}
speak(String name,[String? and,String? chat]){
return "${name}吃${and}${chat}";
}
void main(){
print(eat("张三"));
print(eat("张三",food: "麻辣烫",num: 10));
print(speak("张三"));
print(speak("张三","李四"));
print(speak("张三","李四","聊天"));
}
运行结果:
张三吃null撑了null份
张三吃麻辣烫撑了10份
张三吃nullnull
张三吃李四null
张三吃李四聊天
- 扩展函数
- 常用类型转换 / 时间处理 / 或者Widget的扩展(例如padding)
- 多个扩展函数若已定义 不可重复定义 即函数名不可重复
- dynamic 不可使用扩展函数
/**
* 扩展函数
* 格式:
* extension 函数名 on 类
*
*/
extension toNum on String{
int toInt(){
return int.parse(this);
}
double toDouble(){
return double.parse(this);
}
}
void main(){
var a = "12".toInt();
print(a);
print(a.runtimeType);
dynamic b = "12";
}
结果:
12
int
- 类
- 默认构造函数
- 命名构造函数 Student2.fromJson()
import 'dart:convert';
void main(){
var student = Student("张三", "三年二班", 18, "2005-12-12 12:12:12");
print("student>>${student}");
var student1 = Student1("张三", "三年二班", 18, "2005-12-12 12:12:12");
print("student1>>${student1}");
var student11 = Student1.name("李四"); //命名构造函数
print("student11>>${student11}");
String jsonString = '{"name": "John","classGrade": "三年二班","birthday": "2005-12-12 12:12:12", "age": 30}';
var student2 = Student2.fromJson(jsonString);
print(student2.toString());
}
class Student{
//不添加构造函数 这些属性 会报错 或者可以加上?
String name,classGrade;
int age;
String birthday;
Student(this.name, this.classGrade, this.age, this.birthday);
Map<String,dynamic> toJson(){
return {'name':name,'classGrade':classGrade,'age':age,'birthday':birthday};
}
@override
String toString() {
return 'Student{name: $name, classGrade: $classGrade, age: $age, birthday: $birthday}';
}
}
class Student1{
//不添加构造函数 这些属性 会报错 或者可以加上?
String? name,classGrade;
int? age;
String? birthday;
Student1(this.name, this.classGrade, this.age, this.birthday);
Student1.name(this.name);
@override
String toString() {
return 'Student1{name: $name, classGrade: $classGrade, age: $age, birthday: $birthday}';
}
}
class Student2{
String name = "",classGrade = "";
int age = 0;
String birthday = "";
//命名构造函数
Student2.fromJson(String jsonStr){
var json = jsonDecode(jsonStr);
print(jsonStr);
this.name = json['name'];
this.classGrade = json['classGrade'];
this.age = json['age'];
this.birthday = json['birthday'];
}
@override
String toString() {
return 'Student2{name: $name, classGrade: $classGrade, age: $age, birthday: $birthday}';
}
}
运行结果:
student>>Student{name: 张三, classGrade: 三年二班, age: 18, birthday: 2005-12-12 12:12:12}
student1>>Student1{name: 张三, classGrade: 三年二班, age: 18, birthday: 2005-12-12 12:12:12}
student11>>Student1{name: 李四, classGrade: null, age: null, birthday: null}
{"name": "John","classGrade": "三年二班","birthday": "2005-12-12 12:12:12", "age": 30}
Student2{name: John, classGrade: 三年二班, age: 30, birthday: 2005-12-12 12:12:12}
- 常量构造函数
- 常量构造函数 const修饰构造函数
- 常量构造函数用const 修饰时 属性值一样 则对象相等
import 'dart:convert';
class Student{
String name;
int age;
Student(this.name, this.age);
}
class Student1{
final String name;
final int age;
const Student1(this.name, this.age);
}
void main(){
var student = Student("张三",18);
var student0 = Student("张三",18);
print(student.hashCode);
print(student0.hashCode);
print(student == student0);
print("\n");
var student1 = const Student1("张三",18);
var student11 = const Student1("张三",18);
var student12 = const Student1("张三",19);
print(student1.hashCode);
print(student11.hashCode);
print(student12.hashCode);
print(student1 == student11); //true
print(student12 == student11); //false
}
结果:
333142976
1033297191
false
87118132
87118132
590927718
true
false
- 工厂构造函数
import 'dart:convert';
class Student{
String? name;
int? age;
Student.init({String name ="",int age = 0}){
this.name = name;
this.age = age;
}
static Student instance = Student.init();
factory Student(){
return instance;
}
}
void main(){
var student = Student.instance;
var student1 = Student.instance;
print(student.hashCode);
print(student1.hashCode);
}
结果:
11925193
11925193
- 类
- 默认属性方法修饰符是public
- _下划线表示私有
- 单继承 多实现
class A{
void printA(){
print("A");
}
}
class B{
void printA(){
print("B");
}
}
abstract class M{
void printM(){
print("M");
}
}
abstract class N{
void printN(){
print("N");
}
}
interface class X{
void printX(){
print("X");
}
}
interface class Y{
void printY(){
print("Y");
}
}
//单继承 多实现
class C extends A implements M,N,X,Y{
@override
void printM() {
// TODO: implement printM
}
@override
void printN() {
// TODO: implement printN
}
@override
void printX() {
// TODO: implement printX
}
@override
void printY() {
// TODO: implement printY
}
}
- 类的混入
mixin class P{
void printP(){
print("P");
}
}
mixin class Q{
void printQ(){
print("Q");
}
}
class D with P,Q{
void printD(){
print("D");
}
}
void main(){
var d = D();
d.printD();
d.printP();
d.printQ();
}
结果:
D
P
Q