Skip to content

Instantly share code, notes, and snippets.

@legalcodes
Last active November 27, 2019 18:07
Show Gist options
  • Save legalcodes/25ade03f0632878a9169209e3cd7bef2 to your computer and use it in GitHub Desktop.
Save legalcodes/25ade03f0632878a9169209e3cd7bef2 to your computer and use it in GitHub Desktop.
handle_errors_example
void printOrderMessage () async {
try {
var order = await fetchUserOrder();
print('Awaiting user order...');
print(order);
} catch (err) {
print('Caught error: $err');
}
}
Future<String> fetchUserOrder() {
// Imagine that this function is more complex.
var str = Future.delayed(Duration(seconds: 4), () => throw 'Cannot locate user order');
return str;
}
Future<void> main() async {
await printOrderMessage();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment