6 Matching Annotations
  1. Oct 2022
  2. Jul 2022
    1. future是什么?

      其实 Future 就是对于 Timer 和 microtask 甚至是同步调用的一层封装。这三者的简单用法分别是Future(), Future.microtask(),Future.sync()。

    2. 在异步函数中使用 await 关键字暂停代码的执行,直到对应的 future 完成。

      我理解是否添加 await, 就是把下面函数写在回调里, 还是写在回调外的区别。

    1. 我们看个例子,利用Dart的Isolates实现多线程。

      String sVal = '111';

      void main() async { final rootIsolateReceivePort = ReceivePort(); sVal = '222';

      Isolate spawnIsolate = await Isolate.spawn(spawnIsolateEntryPoint, rootIsolateReceivePort.sendPort); rootIsolateReceivePort.listen((message) { print('current: $sVal, spawn: $message'); }); sVal = '333'; print(sVal); }

      void spawnIsolateEntryPoint(SendPort customCallback) { print(sVal); sVal = '444'; print(sVal); customCallback.send(sVal); }