dartvel 0.1.0
dartvel: ^0.1.0 copied to clipboard
Laravel-native API client for Flutter: contract envelope parsing, typed validation errors, Paginator, Sanctum token lifecycle, device registration.
// Example usage of the dartvel client. See ../README.md for the full API
// surface and error-handling table. Swap `baseUrl` and the `User`/`Post`
// models below for your own app's backend and data.
import 'package:dartvel/dartvel.dart';
Future<void> main() async {
final client = DartvelClient(
baseUrl: 'https://api.example.com/api',
tokenStore: SecureTokenStore(),
localeResolver: () => 'ar',
);
final auth = AuthApi(client: client, decodeUser: User.fromJson);
final user = await auth.login({
'email': 'ahmed@example.com',
'password': 'secret',
});
print('Logged in as ${user.name}');
final posts = await client.getPaginated(
'/posts',
page: const PageRequest(page: 1, perPage: 20),
decodeItem: Post.fromJson,
);
print('Fetched ${posts.items.length} of ${posts.total} posts');
final draft = Post(id: 0, title: 'New post');
try {
await client.post('/posts', body: draft.toJson(), decode: Post.fromJson);
} on ValidationException catch (e) {
print('Validation failed: ${e.errors}');
}
}
class User {
User({required this.id, required this.name});
factory User.fromJson(Object? json) {
final map = json! as Map<String, dynamic>;
return User(id: map['id'] as int, name: map['name'] as String);
}
final int id;
final String name;
}
class Post {
Post({required this.id, required this.title});
factory Post.fromJson(Object? json) {
final map = json! as Map<String, dynamic>;
return Post(id: map['id'] as int, title: map['title'] as String);
}
final int id;
final String title;
Map<String, dynamic> toJson() => {'title': title};
}