Reputation: 535
Imagine im calling an async
function with return type Future<void>
called _uploadData
inside the onPressed
property of a Button
, and the UI does not depend on the function call.
1st
onPressed: () async {
await _uploadData();
},
or 2nd
onPressed: () {
_uploadData();
}
Does it make any difference? And if it doesn't, is the 1st one just the better practice?
Upvotes: 0
Views: 396
Reputation: 855
yes, these codes have different behavior. if you use async/await syntax the code will not continue and will wait until the future completes its work. so If your UI does not depend on Future don't use async/await. otherwise use it. if you want to understand it open the dart-pad counter sample and add below code to it.
void _incrementCounter() async{
await Future.delayed(Duration(seconds: 5));
setState(() {
_counter++;
});
}
convert _incrementCounter() method to above code.
Upvotes: 2