Resolving errors in asynchronous code
There are several ways to handle errors in your asynchronous code. In this recipe, you will see a few examples of dealing with errors, both using the then()
callback and the async/await
pattern.
Getting ready
In order to follow along with this recipe:
- If you followed along with any of the previous recipes in this chapter, you'll only need to edit the existing project. Otherwise, create a new app with the starting code available at https://github.com/PacktPublishing/Flutter-Cookbook/tree/master/chapter_07.
How to do it...
We are going to divide this section into two sub-sections. In the first section, we will deal with the errors by using the then()
callback function and in the second section, we will deal with those errors using the async/await
pattern.
Dealing with errors using the then() callback:
The most obvious way to catch errors in a then()
callback is using the catchError
callback. To do so, follow these steps:
- Add the following...