Want to catch exception in flutter? catchError is sometimes a bit tricky to get right. With async/await you can use try/catch like with sync code and it is usually much easier to get right.
Example:
1 2 3 4 5 6 7 8 9 10 11 | void loginUser(String email, String password) async { try { var user = await _data .userLogin(email, password); _view.onLoginComplete(user); }); } on FetchDataException catch(e) { print('error caught: $e'); _view.onLoginError(); } } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.