Skip to content

Instantly share code, notes, and snippets.

@leconio
Last active November 8, 2021 06:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leconio/73bd75eef9530ff76ac59e09b1331865 to your computer and use it in GitHub Desktop.
Save leconio/73bd75eef9530ff76ac59e09b1331865 to your computer and use it in GitHub Desktop.
dart2练习
// 程序以main开始
// 结尾有分号
// 字符串拼接${}表达式
void main() {
for (int i = 0; i < 5; i++) {
printTest('hello ${i + 1}');
}
}
void printTest(String variableInteger) {
print(variableInteger);
}
// numbers
// strings
// booleans
// lists (also known as arrays)
// maps
// runes (for expressing Unicode characters in a string)
// symbols
void main() {
// numbers
int i = 1;
double d = 1.1;
final p1 = "3";
final p2 = "3.3";
print(int.parse(p1));
print(double.parse(p2));
// string
final str1 = "test";
final str2 = "test";
print(str1 == str2);
final str3 = 'test';
print(str3);
final str4 = '''test''';
print(str4);
print(str4 == str3);
print("I'm ${str4}!!!");
//booleans
if(null) {
print('null is true');
} else {
print('null is false');
}
print(''.isEmpty);
// final list = [];
// if(!list) {
// print('not empty');
// }
}
class Person {
String name;
int age;
String dantengt;
// 注意,冒号后边是初始化列表,用,隔开。
Person() :dantengt = "danm";
@override
String toString() => "name : ${name};age : ${age}";
}
class Person2 {
String name;
int age;
String danteng;
// 关于构造器比较复杂,参考 https://www.dartlang.org/guides/language/language-tour#constructors
// 默认构造器只能声明一个
Person2(this.name,this.age);
// 命名构造器
Person2.copy(Person p) {
this.name = p.name;
this.age = p.age;
}
@override
String toString() => "name : ${name};age : ${age}";
}
class Student extends Person2 {
final String school;
// 普通构造器继承
Student(String name,int age,this.school):super(name,age);
// 命名构造器的继承
Student.copy(Person p,this.school):super.copy(p);
// 类似构造器的重载
Student.mySchoolCopy(Person p):this.copy(p,'mySchool');
}
class ImmutablePoint {
static final ImmutablePoint origin =
const ImmutablePoint(0, 0);
final num x, y;
// cosnt修饰的构造器是常量构造器,里面的值都不许改变。因为是编译时的。
const ImmutablePoint(this.x, this.y);
}
void main() {
final person = Person();
person.name = "spawn";
person.age = 18;
print(person);
final person2 = Person2("lecon",25);
print(person2);
print(Person2.copy(person));
Person2 p2 = Student("lecon",25,"haha");
// is 和 as ;类似 instanceof和强转
if (p2 is Student) {
(p2 as Student).school;
}
}
class Person2 {
String name;
int age;
Person2(this.name,this.age);
// factory 构造器也是构造器,同样不能声明两个相同名字的,而且只能有一个默认
// factory 没有this引用
factory Person2.select(name,int age,int type) {
if(type == 0) {
return Student(name,age,"jaja");
} else {
return Worker(name,age,"lala");
}
}
// callable
call(String a, String b, String c) => '$a $b $c!';
@override
String toString() => "name : ${name};age : ${age}";
}
class Student extends Person2 {
final String school;
Student(String name,int age,this.school):super(name,age);
}
class Worker extends Person2 {
final String company;
Worker(String name,int age,this.company):super(name,age);
}
void main() {
// 不关注Person2实现类,直接使用Person2实例化。使用factory
final p1 = Person2.select("lecon",23,0);
final p2 = Person2.select("lecon",23,1);
print(p1.runtimeType);
print(p2.runtimeType);
// callable
print(p1("123","234","345"));
}
// 所有类型的默认值都是null
void main() {
int i;
String str1;
print(i);
print(str1);
}
void main() {
// throw1();
// throw2();
// throw3();
// throw4();
throw5();
}
void throw1() {
throw FormatException('Expected at least 1 section');
}
void throw2() {
throw 'Expected at least 1 section';
}
void throw3() {
try {
throw FormatException('Expected at least 1 section');
} on FormatException {
print('section exception');
}
try {
throw FormatException('Expected at least 1 section');
} on FormatException catch(e) {
print('section exception ${e}');
}
try {
throw FormatException('Expected at least 1 section');
} on FormatException catch(e,s) {
print('section exception ${e}');
print(s); // 打印方法调用栈
}
}
void throw4() {
try {
throw FormatException('Expected at least 1 section');
} on FormatException {
print('section exception');
rethrow; // 重抛
}
}
void throw5() {
try {
throw FormatException('Expected at least 1 section');
} on FormatException {
print('section exception');
} finally {
print("I am finally");
}
}
// import 'package:meta/meta.dart'
void main() {
func1(123);
func2('222');
func3("lecon");
// func3("lecon","spawn",123); error,使用下边的中括号,注意区别。这个使用key区分,下面使用位置区分
func3("lecon",param2:"spawn",param3:123);
// func4("lecon",param2:"spawn"); error required
func5("lecon","spawn","hahaa");
func6("lecon",age:25);
print(func7());
}
void func1(param1) {
print(param1);
}
void func2(String param2) {
print(param2);
}
void func3(String param1,{String param2,int param3}) {
print("I am ${param1}, Hi ${param2},I am ${param3}");
}
// test in flutter
// void func4(String param1,{String param2,@required int param3}) {
// print("I am ${param1}, Hi ${param2},I am ${param3}");
// }
void func5(String param1,[String param2,String param3]) {
print("I am ${param1}, bian bu ${param2} xia qu le ${param3}");
}
// 只有使用{}和[]的才可以有默认值
void func6(String param1,{int age = 23}) {
print("I am ${param1}, I am ${age}");
}
func7() {
void func8() {
}
void func9() {
}
retrun () => "123";
}
123
222
I am lecon, Hi null,I am null
I am lecon, Hi spawn,I am 123
I am lecon, bian bu spawn xia qu le hahaa
I am lecon, I am 25
null
// 同步异步 https://www.dartlang.org/guides/libraries/library-tour#future
void main() {
print(naturalsTo(10));
print(naturalsDownFrom(10));
asynchronousNaturalsTo(10)
.toList()
.then((res) {
print(res);
});
}
// 同步生成器
Iterable<int> naturalsTo(int n) sync* {
int k = 0;
while (k < n) yield k++;
}
// 异步生成器
Stream<int> asynchronousNaturalsTo(int n) async* {
int k = 0;
while (k < n) yield k++;
}
// 递归性质的生成器
Iterable<int> naturalsDownFrom(int n) sync* {
if (n > 0) {
yield n;
yield* naturalsDownFrom(n - 1);
}
}
import 'package:lib1/lib1.dart';
import 'package:lib2/lib2.dart' as lib2;
// 仅导入foo
import 'package:lib1/lib1.dart' show foo;
// 排除foo
import 'package:lib2/lib2.dart' hide foo;
// 异步导入
import 'package:greetings/hello.dart' deferred as hello;
// https://api.dartlang.org/stable/2.1.0/dart-core/List-class.html
void main() {
// 初始化
final list1 = [1,2,3,4,5];
// 编译时常量
final list2 = const [2,3,4,5,6];
// list2[2] = 8; error
print(list2);
// 初始化2
List<int> list3 = new List(5);
print(list3);
// 初始化3
List<int> list4 = List.filled(3,10);
print(list4);
// 初始化4
List<int> list5 = List.generate(3,(i) => i * 3);
print(list5);
// 更改 使用list1
list1[2] = 8;
print(list1);
// 添加
list1.add(10);
// list1[10] = 20; Index out of range
print(list1);
//删除
list1.remove(10);
print(list1);
}
[2, 3, 4, 5, 6]
[null, null, null, null, null]
[10, 10, 10]
[0, 3, 6]
[1, 2, 8, 4, 5]
[1, 2, 8, 4, 5, 10]
[1, 2, 8, 4, 5]
import 'dart:convert';
// https://api.dartlang.org/stable/2.1.0/dart-core/Map-class.html
void main() {
final map1 = {
"test1key" : "test1value",
// "test1key" : "test1value", Key 不能相同
123 : "123value",
null : 'NULL',
2 : '2'
};
print(map1);
// 获取值
print(map1[123]); // key取值
print(map1[1]); // 下标取值
print(map1[2]); // 同时存在时候,优先Key取值
// 追加值
map1['appendKey'] = 'appendVal';
print(map1);
// 更改值
map1['appendKey'] = 'appendVal123';
print(map1);
map1['appendKey'] = null;
print(map1); // 置位null之后,key不会消失
map1.remove('appendKey');
// 删除
print(map1);
print(map1['non-key']); // null 不会报错
print(map1.length); // 长度
// json 转换
final map2 = const {
"name":"lecon",
"age":23,
"ss":[1,2,3,4,4]
};
final jsonText = jsonEncode(map2);
print(jsonText);
final mapObj = jsonDecode(jsonText);
print(mapObj);
}
{test1key: test1value, 123: 123value, null: NULL, 2: 2}
123value
null
2
{test1key: test1value, 123: 123value, null: NULL, 2: 2, appendKey: appendVal}
{test1key: test1value, 123: 123value, null: NULL, 2: 2, appendKey: appendVal123}
{test1key: test1value, 123: 123value, null: NULL, 2: 2, appendKey: null}
{test1key: test1value, 123: 123value, null: NULL, 2: 2}
null
4
{"name":"lecon","age":23,"ss":[1,2,3,4,4]}
{name: lecon, age: 23, ss: [1, 2, 3, 4, 4]}
mixin Play {
void play() {
print("I can play");
}
}
mixin Eat {
void eat() {
print("I can eat");
}
}
class Person with Play,Eat {
final String name;
final int age;
Person(this.name,this.age);
}
void main() {
final p = Person("lecon",13);
p.play();
p.eat();
}
main() {
var clapping = '\u{1f44f}';
print(clapping);
print(clapping.codeUnits);
print(clapping.runes.toList());
Runes input = new Runes(
'\u2665 \u{1f605} \u{1f60e} \u{1f47b} \u{1f596} \u{1f44d}');
print(new String.fromCharCodes(input));
}
class Rectangle {
num left, top, width, height;
Rectangle(this.left, this.top, this.width, this.height);
num get right => left + width;
set right(num value) => left = value - width;
num get bottom => top + height;
set bottom(num value) => top = value - height;
}
void main() {
var rect = Rectangle(3, 4, 20, 15);
assert(rect.left == 3);
rect.right = 12;
assert(rect.left == -8);
}
void main() {
int l = 4;
String str1 = "str1";
var i = 1;
final j = 2;
const k = 3;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment