flutter_http_provider 2.0.0
flutter_http_provider: ^2.0.0 copied to clipboard
A robust Flutter package for HTTP request handling with advanced file upload capabilities, isolate support, automatic retries, and comprehensive error management.
example/example.dart
import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:flutter_http_provider/flutter_http_provider.dart';
void main() async {
// Initialize the HTTP provider (Singleton)
final httpProvider = FlutterHttpProvider(enableDebugLogs: true);
// Example 1: GET Request
await exampleGetRequest(httpProvider);
// Example 2: POST Request
await examplePostRequest(httpProvider);
// Example 3: File Upload
await exampleFileUpload(httpProvider);
}
/// Example GET request
Future<void> exampleGetRequest(FlutterHttpProvider httpProvider) async {
final response = await httpProvider.httpAccion(
'GET',
'https://jsonplaceholder.typicode.com/posts/1',
false, // runIsolate
'get_post_hash',
'posts',
headers: {
'Content-Type': 'application/json',
},
timeOut: 10,
);
if (response.containsKey('data_ok')) {
debugPrint('GET Success: ${response['data']}');
} else {
debugPrint('GET Error: ${response['error']}');
}
}
/// Example POST request
Future<void> examplePostRequest(FlutterHttpProvider httpProvider) async {
final body = {
'title': 'Test Post',
'body': 'This is a test post body',
'userId': 1,
};
final response = await httpProvider.httpAccion(
'POST',
'https://jsonplaceholder.typicode.com/posts',
false,
'create_post_hash',
'posts',
headers: {
'Content-Type': 'application/json',
},
body: jsonEncode(body),
encoding: Encoding.getByName('utf-8'),
timeOut: 15,
);
if (response.containsKey('data_ok')) {
debugPrint('POST Success: ${response['data']}');
} else {
debugPrint('POST Error: ${response['error']}');
}
}
/// Example file upload with chunks
Future<void> exampleFileUpload(FlutterHttpProvider httpProvider) async {
// Simulated file bytes (in real usage, get these from a file picker)
final Uint8List fileBytes = Uint8List.fromList([0, 1, 2, 3, 4, 5]);
// Create upload session for large files
final uploadSession = httpProvider.createUploadSession(
file: fileBytes,
token: 'your_auth_token',
imagenNombre: 'example_file.jpg',
partSize: 1024 * 1024, // 1MB chunks
urlStreaming: 'https://api.example.com/upload/start',
urlPart: 'https://api.example.com/upload/part/',
);
try {
// Step 1: Start transaction
final transactionId = await uploadSession.iniciarTransaccion();
debugPrint('Transaction started: $transactionId');
// Step 2: Upload file parts
await uploadSession.subirArchivo(
idTransaccion: transactionId,
runIsolate: true, // Use isolate for better performance
timeOutInSeconds: 45,
);
// Step 3: Finalize transaction
final filePath = await uploadSession.finalizarTransaccion(transactionId);
debugPrint('File uploaded to: $filePath');
} catch (error) {
debugPrint('Upload error: $error');
}
}
/// Example using isolates for heavy operations
Future<void> exampleWithIsolate(FlutterHttpProvider httpProvider) async {
final response = await httpProvider.httpAccion(
'GET',
'https://api.example.com/large-data',
true, // runIsolate = true for background execution
'heavy_data_hash',
'large_data',
timeOut: 60,
);
if (response.containsKey('data_ok')) {
debugPrint('Isolate request completed successfully');
}
}