Нет, и при переходе с Java такую связь провести легко. Future в Dart - это значение, которое придёт позже, а не поток. Всё приложение работает в одном изоляте, а у этого изолята один поток, который крутит один цикл событий. Вдобавок async-функция выполняется синхронно до первого await. Внутри filterRows нет ни одного await, так что весь цикл проходит прямо в обработчике нажатия, ещё до того, как Future вообще вернётся. Кадр, который нарисовал бы спиннер, сидит в очереди событий за ним. Перестройка после await тут не медленная часть, она даже не начинается, пока цикл не закончится.
Чтобы убрать работу из этого цикла, нужен второй изолят.
import 'dart:isolate';
List<Entry> filterRows(List<Entry> rows, String query) =>
rows.where((r) => r.matches(query)).toList();
// in the tap handler
final result = await Isolate.run(() => filterRows(rows, query));
Теперь await ждёт сообщения от другого изолята, и цикл событий в это время свободен. Хотелось бы, чтобы ключевое слово меньше звучало так, будто оно уносит работу куда-то ещё.
Переведено с английского·Показать оригиналNope, and it's an easy map to make coming from Java. A Dart Future is a value that arrives later, not a thread. The whole app runs in one isolate, and that isolate has one thread running one event loop. On top of that, an async function runs synchronously until its first await. filterRows has no await inside, so the entire loop runs right there in the tap handler, before the Future is even returned. The frame that would draw the spinner sits in the event queue behind it. The rebuild after the await isn't the slow part, it doesn't even start until the loop is done.
Moving the work off that loop takes a second isolate.
import 'dart:isolate';
List<Entry> filterRows(List<Entry> rows, String query) =>
rows.where((r) => r.matches(query)).toList();
// in the tap handler
final result = await Isolate.run(() => filterRows(rows, query));
Now the await waits for a message from the other isolate, and the event loop is free in between. I do wish the keyword sounded less like it moves work somewhere else.