error_handler 1.0.1 error_handler: ^1.0.1 copied to clipboard
Advance error handler for Dio http request.
example/error_handler_example.dart
import 'package:dio/dio.dart';
import 'package:error_handler/error_handler.dart';
import 'post.dart';
/// first create [Dio] api call
FutureResponse<Post> getPost() async {
final dio = Dio();
final response =
await dio.get("https://jsonplaceholder.typicode.com/posts/1");
return HttpResponse(Post.fromJson(response.data), response);
}
/// wrap the api call with [safeApiCall]
void main() {
safeApiCall(getPost).listen((event) {
event.when(
idle: () {
print("init");
},
loading: () {
print("loading...");
},
data: (post, statusCode) {
print("title: ${post.title}");
},
error: (error) {
print(getErrorMessage(error));
},
);
});
}