callkitnet 1.0.1
callkitnet: ^1.0.1 copied to clipboard
A powerful networking package built on top of Dio with automatic logging, token management, retry support, file upload/download, request cancellation and generic model parsing.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:callkitnet/callkitnet.dart';
void main() {
runApp(
const MyApp(),
);
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
debugShowCheckedModeBanner: false,
home: CallKitNetExampleScreen(),
);
}
}
class User {
final int id;
final String name;
final String email;
User({
required this.id,
required this.name,
required this.email,
});
factory User.fromJson(
Map<String, dynamic> json,
) {
return User(
id: json['id'] as int,
name: json['name'] as String,
email: json['email'] as String,
);
}
}
class CallKitNetExampleScreen extends StatefulWidget {
const CallKitNetExampleScreen({super.key});
@override
State<CallKitNetExampleScreen> createState() =>
_CallKitNetExampleScreenState();
}
class _CallKitNetExampleScreenState
extends State<CallKitNetExampleScreen> {
late final CallKitClient api;
String result = 'Select an example';
@override
void initState() {
super.initState();
api = CallKitClient(
config: const CallKitConfig(
baseUrl: 'https://jsonplaceholder.typicode.com',
),
options: const CallKitOptions(
enableLogs: true,
printCurl: true,
enableRetry: true,
),
);
}
Future<void> getJsonExample() async {
try {
final data = await api.get(
path: '/users/1',
);
setState(() {
result = '''
GET JSON
$data
''';
});
} catch (e) {
setState(() {
result = e.toString();
});
}
}
Future<void> getModelExample() async {
try {
final user = await api.get<User>(
path: '/users/1',
fromJson: User.fromJson,
);
setState(() {
result = '''
GET MODEL
Id: ${user.id}
Name: ${user.name}
Email: ${user.email}
''';
});
} catch (e) {
setState(() {
result = e.toString();
});
}
}
Future<void> getListExample() async {
try {
final users = await api.getList<User>(
path: '/users',
fromJson: User.fromJson,
);
setState(() {
result = '''
GET LIST
Total Users: ${users.length}
First User:
${users.first.name}
${users.first.email}
''';
});
} catch (e) {
setState(() {
result = e.toString();
});
}
}
Future<void> safeGetExample() async {
final response = await api.safeGet<User>(
path: '/users/1',
fromJson: User.fromJson,
);
setState(() {
result = '''
SAFE GET
$response
''';
});
}
Future<void> postExample() async {
try {
final response = await api.post(
path: '/posts',
body: {
'title': 'CallKitNet',
'body': 'Networking Package',
'userId': 1,
},
);
setState(() {
result = '''
POST
${response.data}
''';
});
} catch (e) {
setState(() {
result = e.toString();
});
}
}
Future<void> putExample() async {
try {
final response = await api.put(
path: '/posts/1',
body: {
'id': 1,
'title': 'Updated Title',
'body': 'Updated Body',
'userId': 1,
},
);
setState(() {
result = '''
PUT
${response.data}
''';
});
} catch (e) {
setState(() {
result = e.toString();
});
}
}
Future<void> patchExample() async {
try {
final response = await api.patch(
path: '/posts/1',
body: {
'title': 'Patched Title',
},
);
setState(() {
result = '''
PATCH
${response.data}
''';
});
} catch (e) {
setState(() {
result = e.toString();
});
}
}
Future<void> deleteExample() async {
try {
final response = await api.delete(
path: '/posts/1',
);
setState(() {
result = '''
DELETE
${response.data}
''';
});
} catch (e) {
setState(() {
result = e.toString();
});
}
}
void saveTokenExample() {
api.saveToken(
'sample_access_token',
);
setState(() {
result = '''
TOKEN SAVED
sample_access_token
''';
});
}
void clearTokenExample() {
api.clearToken();
setState(() {
result = '''
TOKEN CLEARED
''';
});
}
Future<void> requestCancellationExample() async {
try {
api.get(
path: '/users',
tag: 'users_request',
);
await Future.delayed(
const Duration(
milliseconds: 100,
),
);
RequestCanceller.cancel(
'users_request',
);
setState(() {
result = '''
REQUEST CANCELLATION
Request with tag "users_request"
was cancelled successfully.
''';
});
} catch (e) {
setState(() {
result = e.toString();
});
}
}
Future<void> uploadExample() async {
setState(() {
result = '''
UPLOAD EXAMPLE
await api.upload.uploadFile(
path: '/upload',
file: imageFile,
);
OR
await api.upload.uploadFiles(
path: '/upload',
files: [
image1,
image2,
],
);
''';
});
}
Future<void> downloadExample() async {
setState(() {
result = '''
DOWNLOAD EXAMPLE
await api.download.download(
url: fileUrl,
savePath: savePath,
);
With Progress:
await api.download.download(
url: fileUrl,
savePath: savePath,
onProgress: (
received,
total,
) {
print(
'\${(received / total * 100).toStringAsFixed(0)}%',
);
},
);
''';
});
}
Widget buildButton({
required String title,
required VoidCallback onPressed,
}) {
return Padding(
padding: const EdgeInsets.all(
4,
),
child: ElevatedButton(
onPressed: onPressed,
child: Text(
title,
),
),
);
}
@override
Widget build(
BuildContext context,
) {
return Scaffold(
appBar: AppBar(
title: const Text(
'CallKitNet Examples',
),
),
body: Column(
children: [
Expanded(
child: SingleChildScrollView(
padding: const EdgeInsets.all(
16,
),
child: SelectableText(
result,
),
),
),
const Divider(),
SizedBox(
height: 350,
child: GridView.count(
crossAxisCount: 2,
childAspectRatio: 3,
children: [
buildButton(
title: 'GET JSON',
onPressed: getJsonExample,
),
buildButton(
title: 'GET MODEL',
onPressed: getModelExample,
),
buildButton(
title: 'GET LIST',
onPressed: getListExample,
),
buildButton(
title: 'SAFE GET',
onPressed: safeGetExample,
),
buildButton(
title: 'POST',
onPressed: postExample,
),
buildButton(
title: 'PUT',
onPressed: putExample,
),
buildButton(
title: 'PATCH',
onPressed: patchExample,
),
buildButton(
title: 'DELETE',
onPressed: deleteExample,
),
buildButton(
title: 'SAVE TOKEN',
onPressed: saveTokenExample,
),
buildButton(
title: 'CLEAR TOKEN',
onPressed: clearTokenExample,
),
buildButton(
title: 'CANCEL REQUEST',
onPressed: requestCancellationExample,
),
buildButton(
title: 'UPLOAD',
onPressed: uploadExample,
),
buildButton(
title: 'DOWNLOAD',
onPressed: downloadExample,
),
],
),
),
],
),
);
}
}