Reputation: 1095
How to structure the following code correctly? I launch an expensive async operation. The "runExpensiveOperation" method stops the spinning CircularProgressIndicator so it seems it is executed on the main thread. Is there anything like "dispatch_async" like on native 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,
)
],
),
),
);
}
}
Upvotes: 6
Views: 5248
Reputation: 268134
class _MyHomePageState extends State<MyHomePage> {
void _pressed() async {
print("I am pressed");
print("I will make a cheap operation now");
// string returned is "Good Morning"
String string = await compute(runExpensiveOperation, null); // this is what you need
}
@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,
)
],
),
),
);
}
}
// make sure this is outside your class definition
Future<String> runExpensiveOperation(void _) 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');
return "Good Morning";
}
Upvotes: 6