data_handler 0.0.5
data_handler: ^0.0.5 copied to clipboard
A Flutter package for managing API states, loading, error handling, and data retrieval with enhanced global configuration.
import 'package:data_handler/data_handler.dart';
import 'package:example/interceptor/logging_interceptor.dart';
import 'package:example/screen/public_api_screen.dart';
import 'package:example/theme/app_theme.dart';
import 'package:flutter/material.dart';
void main() {
// Setup global widgets, error formatting, and team interceptors before running the app
_setupGlobalWidgets();
runApp(const MyApp());
}
/// Setup global widgets, error formatting, and team interceptors for consistent UI across the app
void _setupGlobalWidgets() {
// 1. Centralized Team Error Formatter
DataHandlerConfig.setErrorFormatter((error) {
final errStr = error.toString();
if (errStr.contains('SocketException') ||
errStr.contains('ClientException')) {
return 'No internet connection. Please verify your network and retry.';
}
if (errStr.contains('TimeoutException')) {
return 'Request timed out. The server took too long to respond.';
}
return errStr.replaceFirst('Exception: ', '');
});
// 2. App-wide Interceptor for telemetry and logging
DataHandlerConfig.addInterceptor(AppLoggingInterceptor.instance);
// 3. Global UI Builders
DataHandlerConfig.setGlobalWidgets(
loadingWidget: () => const Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
CircularProgressIndicator(
strokeWidth: 3,
valueColor: AlwaysStoppedAnimation<Color>(Color(0xFF6C5CE7)),
),
SizedBox(height: 20),
Text(
'Loading...',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w500,
color: Colors.grey,
),
),
],
),
),
errorWidget: (error) => Center(
child: Container(
margin: const EdgeInsets.all(24),
padding: const EdgeInsets.all(24),
decoration: BoxDecoration(
color: Colors.red.shade50,
borderRadius: BorderRadius.circular(16),
border: Border.all(color: Colors.red.shade200),
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Icon(
Icons.error_outline_rounded,
size: 64,
color: Colors.red.shade400,
),
const SizedBox(height: 16),
const Text(
'Oops! Something went wrong',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Colors.red,
),
),
const SizedBox(height: 12),
Text(
error,
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 14,
color: Colors.red.shade700,
height: 1.5,
),
),
],
),
),
),
emptyWidget: (message) => Center(
child: Container(
margin: const EdgeInsets.all(24),
padding: const EdgeInsets.all(32),
decoration: BoxDecoration(
color: Colors.grey.shade100,
borderRadius: BorderRadius.circular(16),
border: Border.all(color: Colors.grey.shade300),
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Icon(
Icons.inbox_outlined,
size: 80,
color: Colors.grey.shade400,
),
const SizedBox(height: 20),
Text(
message.isEmpty ? 'No data available' : message,
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.w500,
color: Colors.grey.shade600,
),
),
const SizedBox(height: 8),
Text(
'Pull to refresh or try again',
style: TextStyle(
fontSize: 14,
color: Colors.grey.shade500,
),
),
],
),
),
),
);
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
bool _isDarkMode = false;
void _toggleTheme() {
setState(() {
_isDarkMode = !_isDarkMode;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Enhanced Data Handler Example',
debugShowCheckedModeBanner: false,
themeMode: _isDarkMode ? ThemeMode.dark : ThemeMode.light,
theme: AppTheme.buildLightTheme(),
darkTheme: AppTheme.buildDarkTheme(),
home:
PublicApiExample(toggleTheme: _toggleTheme, isDarkMode: !_isDarkMode),
);
}
}