Exception Handling in asynchronous code
Exception Handling with then (catchError)
void main() {
//Exception Handling
print(" Start fetching images");
getData().then((images) {
return images + " " + "caption";
}).then((captionedimages) {
print(captionedimages + " " + " save to disk");
}).catchError((err){
// Logic for handling error
print("Error : $err , Sorry for the error , check if you are connected to internet");
});
print("Loading Images");
}
Future<String> getData() {
Future<String> a = Future<String>.delayed(Duration(seconds: 5), () {
// Logic
print(" Image Fetched !!!!!");
throw "404 Error";
});
return a;
}
If you want to read more about error handling , here is the official documentation
Exception Handling with async await( try catch block)
You should go through the following codelab to get a better understanding of asynchronous programming in dart :-
Last updated
Was this helpful?