appliteui_core 0.0.14 copy "appliteui_core: ^0.0.14" to clipboard
appliteui_core: ^0.0.14 copied to clipboard

unlisted

A Flutter package for making API calls with support for interceptors, error handling, and typed responses. Built on top of Dio for robust HTTP operations.

appliteui_core #

A Flutter package for making API calls with support for interceptors, error handling, and typed responses.

Features #

  • 🚀 Simple API client based on Dio
  • 🔒 Built-in authentication interceptor
  • 📝 Request/response logging interceptor
  • ⚠️ Comprehensive error handling
  • 🎯 Typed responses with generics
  • 🔄 Support for all HTTP methods (GET, POST, PUT, PATCH, DELETE)

Installation #

Add this to your package's pubspec.yaml file:

dependencies:
  appliteui_core:
    path: ../appliteui_core

Usage #

Basic Setup #

import 'package:appliteui_core/appliteui_core.dart';

// Create API client
final apiClient = ApiClient(
  baseUrl: 'https://api.example.com',
  headers: {
    'Content-Type': 'application/json',
  },
);

With Interceptors #

// Create client with authentication and logging
final apiClient = ApiClient(
  baseUrl: 'https://api.example.com',
  interceptors: [
    AuthInterceptor(getToken: () => 'your-token-here'),
    LoggingInterceptor(),
  ],
);

Making Requests #

GET Request

try {
  final response = await apiClient.get<Map<String, dynamic>>(
    '/users/1',
    fromJson: (json) => json as Map<String, dynamic>,
  );
  
  if (response.isSuccess) {
    print('User data: ${response.data}');
  }
} on ApiException catch (e) {
  print('Error: ${e.message}');
}

POST Request

try {
  final response = await apiClient.post<Map<String, dynamic>>(
    '/users',
    data: {
      'name': 'John Doe',
      'email': 'john@example.com',
    },
    fromJson: (json) => json as Map<String, dynamic>,
  );
  
  print('Created user: ${response.data}');
} on ApiException catch (e) {
  print('Error: ${e.message}');
}

PUT Request

try {
  final response = await apiClient.put<Map<String, dynamic>>(
    '/users/1',
    data: {
      'name': 'Jane Doe',
    },
    fromJson: (json) => json as Map<String, dynamic>,
  );
  
  print('Updated user: ${response.data}');
} on ApiException catch (e) {
  print('Error: ${e.message}');
}

DELETE Request

try {
  await apiClient.delete('/users/1');
  print('User deleted');
} on ApiException catch (e) {
  print('Error: ${e.message}');
}

Error Handling #

The package provides structured error handling through ApiException:

try {
  final response = await apiClient.get('/endpoint');
} on ApiException catch (e) {
  switch (e.type) {
    case ApiExceptionType.network:
      print('Network error: ${e.message}');
      break;
    case ApiExceptionType.timeout:
      print('Timeout: ${e.message}');
      break;
    case ApiExceptionType.server:
      print('Server error (${e.statusCode}): ${e.message}');
      break;
    case ApiExceptionType.cancel:
      print('Request cancelled');
      break;
    case ApiExceptionType.unknown:
      print('Unknown error: ${e.message}');
      break;
  }
}

Custom Model Parsing #

class User {
  final int id;
  final String name;
  
  User({required this.id, required this.name});
  
  factory User.fromJson(Map<String, dynamic> json) {
    return User(
      id: json['id'],
      name: json['name'],
    );
  }
}

// Use with API client
final response = await apiClient.get<User>(
  '/users/1',
  fromJson: (json) => User.fromJson(json),
);

print('User: ${response.data.name}');

API Reference #

ApiClient #

Main class for making API requests.

Methods:

  • get<T>() - Make GET request
  • post<T>() - Make POST request
  • put<T>() - Make PUT request
  • patch<T>() - Make PATCH request
  • delete<T>() - Make DELETE request
  • addInterceptor() - Add interceptor
  • removeInterceptor() - Remove interceptor
  • clearInterceptors() - Clear all interceptors

ApiResponse #

Generic response wrapper.

Properties:

  • data - Response data
  • statusCode - HTTP status code
  • message - Response message
  • isSuccess - Whether request was successful

ApiException #

Custom exception for API errors.

Properties:

  • message - Error message
  • statusCode - HTTP status code (if applicable)
  • type - Type of error (ApiExceptionType)

Interceptors #

AuthInterceptor

Adds authentication headers to requests.

AuthInterceptor(getToken: () => 'your-token')

LoggingInterceptor

Logs requests and responses for debugging.

LoggingInterceptor(
  logRequest: true,
  logResponse: true,
  logError: true,
)

License #

This project is licensed under the MIT License.

0
likes
125
points
62
downloads

Documentation

API reference

Publisher

verified publisherfreddydro.dev

Weekly Downloads

A Flutter package for making API calls with support for interceptors, error handling, and typed responses. Built on top of Dio for robust HTTP operations.

Repository (GitHub)
View/report issues

License

MIT (license)

Dependencies

adaptive_dialog, applite_image_picker, dio, firebase_storage, flutter, flutter_easyloading, flutter_image_compress, hive_ce, hive_ce_flutter, http

More

Packages that depend on appliteui_core