No top-level method 'spawnFunction' declar

2019-09-17 08:47发布

问题:

I'm trying to use Isolates in Dart. The tutorials from dartlang.org seem to use the function spawnFunction. But that does not seem to work for me. And I cant find any docs about this.

import 'dart:isolate';

void doThing() {
  print('Hello!');
}

main() {
  spawnFunction(doThing);
}

.

Unhandled exception:
No top-level method 'spawnFunction' declared.

The docs from api.dartlang.org mention Isolate.spawn but I get an error saying there is no static method spawn declared.

Did I miss something? A link to appropriate docs (if any) would be appreciated.

Thanks!

回答1:

Isolate.spawn is indeed the new way of creating isolates. Your example would need to be rewritten as:

import 'dart:isolate';

void doThing(_) {
  print("Hello!");
}

main() {
  Isolate.spawn(doThing, null);
}

See https://groups.google.com/a/dartlang.org/forum/#!topic/misc/EVUMkZXFXtY for the breaking change announcement.



标签: dart