When you use the async/await pattern, you can just handle errors with the try… catch syntax, exactly like you would when dealing with synchronous code. Refactor the handleError() method as shown here:
Future handleError() async {
try {
await returnError();
}
catch (error) {
setState(() {
result = error;
});
}
finally {
print('Complete');
}
}
If you call handleError() in the onPressed method of the ElevatedButton, you should see that the catch was called, and the result is exactly the same as in the catchError callback.