Как правильно структурировать следующий код? Я запускаю дорогую асинхронную операцию. Метод «runExорогоOperation» останавливает вращающийся CircularProgressIndicator, поэтому кажется, что он выполняется в основном потоке. Есть ли что-то вроде dispatch_async, как на родной iOS?
class _MyHomePageState extends State<MyHomePage> {
void _pressed() async {
print("I am pressed");
print("I will make a cheap operation now");
await runExpensiveOperation();
}
Future<void> runExpensiveOperation() async {
print('starting expensive operation');
for (var i = 0; i<1000000; i++) {
List<int> bytes = utf8.encode('somethingtohash');
String hash = sha256.convert(bytes).toString();
}
print('finished expensive operation');
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
CircularProgressIndicator(),
FlatButton(
child: Text("Press me for tough operation"),
onPressed: _pressed,
)
],
),
),
);
}
}