error_handler 1.0.2-dev copy "error_handler: ^1.0.2-dev" to clipboard
error_handler: ^1.0.2-dev copied to clipboard

outdated

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));
      },
    );
  });
}