error_handler 1.0.4 error_handler: ^1.0.4 copied to clipboard
error handler for all http client in dart like dio, chopper http and more
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 {
const path = "https://jsonplaceholder.typicode.com/posts/1";
final response = await Dio().get(path);
return response.convert(Post.fromJson);
}
/// wrap the api call with [ErrorHandler.future]
Future<void> main() async {
final state = await errorHandler.future(getPost);
state.whenOrNull(
data: (post, response) {
print("title: ${post.title}");
},
error: (error) {
print(getErrorMessage(error));
},
);
}