lynk_io 0.0.2
lynk_io: ^0.0.2 copied to clipboard
A lightweight, modular, and generic Flutter networking library that acts as a unified communication layer between your app and remote APIs. Built on top of Dio and inspired by clean architecture princ [...]
# 🔗 lynk_io
A scalable and reusable API communication layer built with Dio for Flutter apps.
Designed to simplify API calls, error handling, and
dependency setup, following Clean Architecture and modular design.
---
## 🚀 Features
- Generic GET, POST, PUT, DELETE support
- Clean separation of concerns using SOLID principles
- Global error handling with proper exceptions
- Simple logging with pluggable logger
- Plug-and-play dependency injection via `get_it`
- Supports `.env` based API URL configuration (cross-platform)
- Easily extensible for token/auth-based flows
---
## 📦 Installation
Add this to your `pubspec.yaml`:
```yaml
dependencies:
lynk_io: ^1.0.0
flutter_dotenv: ^5.1.0
🧱 Environment Setup #
To dynamically manage environments like dev, staging, and production:
- Create a
.envfile at the root of your Flutter app:
API_BASE_URL=https://jsonplaceholder.typicode.com
- Register the
.envfile in yourpubspec.yaml:
flutter:
assets:
- .env
- Load the environment and set up dependencies before
runApp():
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:lynk_io/lynk_io.dart';
void main() async {
await dotenv.load();
final baseUrl = dotenv.env['API_BASE_URL'] ?? '';
setupDependencies(baseUrl: baseUrl);
runApp(MyApp());
}
📦 Example API Usage #
Define a model class in your app layer:
class User {
final int id;
final String name;
User({required this.id, required this.name});
factory User.fromJson(Map<String, dynamic> json) =>
User(id: json['id'], name: json['name']);
}
Fetch data using the reusable client:
final api = locator<ApiClient>();
final result = await api.get<User>(
endpoint: '/users/1',
fromJson: User.fromJson,
);
if (result.isSuccess) {
AppLogger.log("User: ${result.data!.name}");
} else {
AppLogger.error("Error: ${result.error!.message}");
}
📤 POST Request Example #
Sending a new user with POST:
final api = locator<ApiClient>();
final result = await api.post<User>(
endpoint: '/users',
body: {
'name': 'John Doe',
'email': 'john@example.com',
},
fromJson: User.fromJson,
);
if (result.isSuccess) {
AppLogger.log("Created User: ${result.data!.name}");
} else {
AppLogger.error("Error: ${result.error!.message}");
}
🔄 Other Supported Methods #
The package provides clean wrappers for all common REST methods:
✅ GET<T>() #
api.get<T>(
endpoint: '/items',
fromJson: T.fromJson,
);
✅ POST<T>() #
api.post<T>(
endpoint: '/items',
body: {...},
fromJson: T.fromJson,
);
✅ PUT<T>() #
api.put<T>(
endpoint: '/items/1',
body: {...},
fromJson: T.fromJson,
);
✅ DELETE<T>() #
api.delete<T>(
endpoint: '/items/1',
fromJson: T.fromJson,
);
These methods automatically handle:
- JSON serialization with
fromJson - Standardized
ApiResponse<T>result - Error parsing into
ApiError - Logging of request/response lifecycle
👨💻 Author #
Built and maintained with ❤️ by Siva G.