-
Notifications
You must be signed in to change notification settings - Fork 1
Home
M Atheer edited this page Sep 16, 2022
·
1 revision
import 'package:dio/dio.dart';
import 'package:error_handler/error_handler.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));
},
);
}