httpio_client 1.0.2
httpio_client: ^1.0.2 copied to clipboard
A powerful, unified Flutter networking package for Dio and http.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:httpio_client/httpio_client.dart';
import 'dart:convert';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'HTTPio Client Demo',
theme: ThemeData(
primarySwatch: Colors.indigo,
useMaterial3: true,
),
home: const ExampleHomePage(),
);
}
}
class ExampleHomePage extends StatefulWidget {
const ExampleHomePage({super.key});
@override
State<ExampleHomePage> createState() => _ExampleHomePageState();
}
class _ExampleHomePageState extends State<ExampleHomePage> {
late NetworkClient _client;
ClientType _selectedClient = ClientType.dio;
String _result = 'Press button to make a request';
bool _loading = false;
String _currentPath = '';
@override
void initState() {
super.initState();
_initClient();
}
void _initClient() {
final config = NetworkConfig(
clientType: _selectedClient,
baseUrl: 'https://jsonplaceholder.typicode.com',
retryConfig: RetryConfig(maxRetries: 3),
enableLogging: true,
);
_client = HttpioClient.create(config);
}
Future<void> _runRequest(String type) async {
setState(() {
_loading = true;
_result = 'Running $type request...';
});
ApiResponse response;
final startTime = DateTime.now();
try {
switch (type) {
case 'GET':
_currentPath = '/posts/1';
response = await _client.get<Map<String, dynamic>>(_currentPath);
break;
case 'POST':
_currentPath = '/posts';
response = await _client.post<Map<String, dynamic>>(
_currentPath,
data: {'title': 'HTTPio', 'body': 'Modern Networking', 'userId': 1},
);
break;
case 'PUT':
_currentPath = '/posts/1';
response = await _client.put<Map<String, dynamic>>(
_currentPath,
data: {'id': 1, 'title': 'Updated Title', 'body': 'Updated Body'},
);
break;
case 'PATCH':
_currentPath = '/posts/1';
response = await _client.patch<Map<String, dynamic>>(
_currentPath,
data: {'title': 'Patched Title'},
);
break;
case 'DELETE':
_currentPath = '/posts/1';
response = await _client.delete<Map<String, dynamic>>(_currentPath);
break;
case 'MULTIPART':
_currentPath = '/posts';
response = await _client.multipart(
_currentPath,
data: {
'name': 'Test Upload',
'info': 'test upload data',
},
);
break;
default:
return;
}
final duration = DateTime.now().difference(startTime).inMilliseconds;
setState(() {
_loading = false;
final responseMap = response.toJson();
_result = 'Client: ${_selectedClient.name.toUpperCase()}\n'
'Result: ${response.result} ($duration ms)\n'
'Path: $_currentPath\n\n'
'${const JsonEncoder.withIndent(' ').convert(responseMap)}';
});
} catch (e) {
setState(() {
_loading = false;
_result = 'Exception: $e';
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('HTTPio Request Tester'),
backgroundColor: Colors.indigo,
foregroundColor: Colors.white,
actions: [
Container(
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 4),
margin: const EdgeInsets.symmetric(vertical: 8),
decoration: BoxDecoration(
color: Colors.white.withOpacity(0.2),
borderRadius: BorderRadius.circular(8),
),
child: DropdownButtonHideUnderline(
child: DropdownButton<ClientType>(
value: _selectedClient,
dropdownColor: Colors.indigo,
iconEnabledColor: Colors.white,
style: const TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
),
items: ClientType.values.map((type) {
return DropdownMenuItem(
value: type,
child: Text(type.name.toUpperCase()),
);
}).toList(),
onChanged: (value) {
if (value != null) {
setState(() {
_selectedClient = value;
_initClient();
});
}
},
),
),
),
const SizedBox(width: 16),
],
),
body: Column(
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Wrap(
spacing: 8,
children: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'MULTIPART']
.map((type) => ActionChip(
label: Text(type),
onPressed: _loading ? null : () => _runRequest(type),
backgroundColor:
_getColorForMethod(type).withOpacity(0.2),
))
.toList(),
),
),
const Divider(),
Expanded(
child: Container(
width: double.infinity,
padding: const EdgeInsets.all(16),
color: Colors.grey[900],
child: SingleChildScrollView(
child: _loading
? const Center(child: CircularProgressIndicator())
: Text(
_result,
style: const TextStyle(
color: Colors.greenAccent,
fontFamily: 'monospace',
),
),
),
),
),
],
),
);
}
Color _getColorForMethod(String method) {
switch (method) {
case 'GET':
return Colors.green;
case 'POST':
return Colors.blue;
case 'PUT':
return Colors.orange;
case 'PATCH':
return Colors.purple;
case 'DELETE':
return Colors.red;
case 'MULTIPART':
return Colors.teal;
default:
return Colors.grey;
}
}
}