Flutter 学习(三) dart基础

1,075 阅读3分钟

dart 语言的异步 同步 构造器 隔离 元注解 注释方式

1. async await

下面依次调用 三个方法 输出的顺序为1--3--4--2--5await该关键字会执行暂停当前线程 等外部执行完毕再执行剩下的

main() async {
  // async wait
  getName1();
  getName2();
  getName3();
  }
/ async wait
Future<void> getName1() async {
//  getStr1();//可以不用await打断点看下await后的区别
  await getStr1(); //遇到第一个await表达式执行暂停,返回future对象,await表达式执行完成后继续执行
  await getStr2(); //await表达式可以使用多次
  print('5');
}

getStr1() {
  print('1');
}

getStr2() {
  print('2');
}

getName2() {
  print('3');
}

getName3() {
  print('4');
}

2. 队列添加

  • Future

每次新建一个Future 会将这个Future添加到队列当中 先添加先执行 下面代码的输入顺序为f7--f1--f6--f3--f5--ff2--f4

void testFuture() {
  Future f = new Future(() => print("f1"));
  Future f1 = new Future(() => null); //7163524
//  Future f1 = new Future.delayed(Duration(seconds: 1) ,() => null);//7132465
  Future f2 = new Future(() => null);
  Future f3 = new Future(() => null);

  f3.then((_) => print("f2"));
  f2.then((_) {
    print("f3");
    new Future(() => print("f4"));
    f1.then((_) {
      print("f5");
    });
  });

  f1.then((m) {
    print("f6");
  });
  print("f7");
}
  • scheduleMicrotask 微队列

微队列会优先与队列执行,微队列执行会在当前正在执行的Future任务 执行完才会去执行微队列的任务, 如果then返回一个future任务那这个任务也会放到队列中, delayed延迟队列会在最后执行 下面的代码输出顺序为s9--s1--s8--s3--s4--s6--s5--s10--s7--s11--s12--s2

void testScheduleMicrotask() {
  //918346572
  scheduleMicrotask(() => print('s1'));

  new Future.delayed(new Duration(seconds: 1), () => print('s2'));

  new Future(() => print('s3')).then((_) {
    print('s4');
    scheduleMicrotask(() => print('s5'));
  }).then((_) => print('s6'));

  new Future(() => print('s10'))
      .then((_) => new Future(() => print('s11')))
      .then((_) => print('s12'));

  new Future(() => print('s7'));

  scheduleMicrotask(() => print('s8'));

  print('s9');
}

3. isolates 隔离

所有的 Dart 代码在 isolates 中运行而不是线程。 每个 isolate 都有自己的堆内存,并且确保每个 isolate 的状态都不能被其他 isolate 访问。 其实 一个main方法就是一个隔离


main() async {
  var receivePort = new ReceivePort();
  await Isolate.spawn(echoo, receivePort.sendPort);
  receivePort.listen((s) {
    if (s is SendPort) {
      s.send("不我想要桃子");
    } else
      print(s);
  });
}

/// 新isolate的入口函数
echoo(SendPort sendPort) async {
  // 实例化一个ReceivePort 打开接收端口以接收消息
  var port = new ReceivePort();

  // 把它的sendPort发送给宿主isolate,以便宿主可以给它发送消息
  sendPort.send(port.sendPort);
  sendPort.send("给你烂苹果");
  // 监听循环接收消息
  port.listen((s) {
    print(s);
  });
}

4. 生成器

sync* async用于生成 Iterrable 和 Stream yield yield 优化性能

  • 同步生成器

返回值是Iterable 需要调用 moveNext() 才会执行迭代


  //调用getSyncGenerator立即返回Iterable
  var it = getSyncGenerator(5).iterator;
//  调用moveNext方法时getSyncGenerator才开始执行
  while (it.moveNext()) {
    print(it.current);
  }
  
  
//同步生成器: 使用sync*,返回的是Iterable对象
Iterable<int> getSyncGenerator(int n) sync* {
  print('start');
  int k = n;
  while (k > 0) {
    //yield会返回moveNext为true,并等待 moveNext 指令
    yield k--;
  }
  print('end');
}

  • 异步生成器

返回值是Stream 执行了只有执行了listen之后函数才会执行,也可以使用可以使用StreamSubscription对象对数据流进行控制


 //调用getAsyncGenerator立即返回Stream,只有执行了listen,函数才会开始执行
//  getAsyncGenerator(5).listen((value) => print(value));


  StreamSubscription subscription = getAsyncGenerator(5).listen(null);
  subscription.onData((value) {
    print(value);
    if (value >= 2) {
      subscription.pause(); //可以使用StreamSubscription对象对数据流进行控制
    }
 });

//异步生成器: 使用async*,返回的是Stream对象
Stream<int> getAsyncGenerator(int n) async* {
  print('start');
  int k = 0;
  while (k < n) {
    //yield不用暂停,数据以流的方式一次性推送,通过StreamSubscription进行控制
    yield k++;
  }
  print('end');
}

  • 递归生成器
//同步
  var it1 = getSyncRecursiveGenerator(5).iterator;
  while (it1.moveNext()) {
    print(it1.current);
  }
  //异步
  getAsyncRecursiveGenerator(5).listen((value) => print(value));


//递归生成器:使用yield*
Iterable<int> getSyncRecursiveGenerator(int n) sync* {
  if (n > 0) {
    yield n;
    yield* getSyncRecursiveGenerator(n - 1);
  }
}

//异步递归生成器
Stream<int> getAsyncRecursiveGenerator(int n) async* {
  if (n > 0) {
    yield n;
    yield* getAsyncRecursiveGenerator(n - 1);
  }
}

5. 元注解

  • @deprecated 过时
  • @override 重写
  • 自定义注解
class Todo {
  final String who;
  final String what;

  const Todo({this.who, this.what});
}

6. 注释方式

  • 这是单行注释
// 这是单行注释
  • 多行注释
/*
 * 这是多行注释
 * 这是多行注释 
 */

  • 文档注释

两种方式
/// 这是文档注释

/**
  * 这是文档注释
  */