Documentation • Issues • Example • License • Pub.dev
A lightweight and flexible Dart HTTP client designed for making API calls with dynamic response parsing. It provides an easy way to handle HTTP requests and parse JSON responses into specific data models.
- Simple API: Includes basic HTTP methods like
GET,POST, andDELETE. - Dynamic Parsing: Supports generic types for flexible JSON-to-object conversion.
- Error Handling: Encapsulates success and error states using the
Result<T>class. - No Inheritance: Avoids unnecessary inheritance from
http.Clientfor a minimal and focused API.
Add the following to your pubspec.yaml:
dependencies:
tarsier_http_parser: ^1.0.1Run the following command:
flutter pub getCreate a data model that includes a fromJson factory constructor:
class User {
final String id;
final String name;
User({required this.id, required this.name});
factory User.fromJson(Map<String, dynamic> json) {
return User(
id: json['id'] as String,
name: json['name'] as String,
);
}
}Create an instance of TarsierHttpClient with the fromJson function for single object:
final client = TarsierHttpClient<User>(
fromJson: (json) => User.fromJson(json as Map<String, dynamic>),
);For a list of objects:
final client = TarsierHttpClient<List<User>>(
fromJson: (json) => (json as List).map((item) => User.fromJson(item)).toList(),
);GET Request
final result = await client.get(Uri.parse('https://api.example.com/user'));
result
.onSuccess((user) => print('User: ${user.name}'))
.onError((error) => print('Error: $error'));POST Request
final postResult = await client.post(
Uri.parse('https://api.example.com/user'),
body: {'name': 'New User'},
);
postResult
.onSuccess((user) => print('Created User: ${user.name}'))
.onError((error) => print('Error: $error'));DELETE Request
final deleteResult = await client.delete(Uri.parse('https://api.example.com/user/123'));
deleteResult
.onSuccess((data) => print('User deleted'))
.onError((error) => print('Error: $error'));This project is licensed under the MIT License. See the LICENSE file for details.
Contributions are welcome! Please submit a pull request or file an issue for any bugs or feature requests on GitHub.
The tarsier, one of the smallest primates, symbolizes simplicity and adaptability—just like this package! 🐒