flutter_smart_error_handler 1.0.1
flutter_smart_error_handler: ^1.0.1 copied to clipboard
A comprehensive Flutter error handler that catches and categorizes errors with beautiful Lottie animations.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:flutter_smart_error_handler/flutter_smart_error_handler.dart';
import 'dart:io';
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
void main() {
// Initialize the global error handler with navigator key
GlobalErrorHandler.initialize(navigatorKey: navigatorKey);
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
navigatorKey: navigatorKey, // Important for async error navigation
title: 'Flutter Error Handler Demo',
theme: ThemeData(primarySwatch: Colors.blue),
home: const MyHomePage(title: 'Advanced Error Handler Demo'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(widget.title)),
body: SingleChildScrollView(
child: Center(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Wrap(
spacing: 10,
runSpacing: 10,
alignment: WrapAlignment.center,
children: <Widget>[
const Text(
'Press buttons to test specific Error Types:',
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
),
const SizedBox(width: double.infinity, height: 10),
// 1. Network
_ErrorButton(
label: 'Network Error',
color: Colors.blue,
onPressed: () async =>
throw const SocketException('Failed host lookup'),
),
// 2. Auth
_ErrorButton(
label: 'Auth Error',
color: Colors.orange,
onPressed: () async =>
throw Exception('401 Unauthorized access'),
),
// 3. Backend
_ErrorButton(
label: 'Server Error',
color: Colors.redAccent,
onPressed: () async =>
throw Exception('500 Internal Server Error'),
),
// 4. App Logic
_ErrorButton(
label: 'Null Pointer',
color: Colors.purple,
onPressed: () async => throw const FormatException(
'Invalid format',
), // Simulating logic error
),
// 5. Input
_ErrorButton(
label: 'Input Error',
color: Colors.teal,
onPressed: () async {
// Simulating validation error handled via exception
throw Exception('Invalid Email Address input');
// Note: Usually input errors aren't thrown as global exceptions,
// but for checking the UI:
},
),
// 6. Device
_ErrorButton(
label: 'Camera Permission',
color: Colors.brown,
onPressed: () async =>
throw Exception('Camera permission denied'),
),
// 7. Storage
_ErrorButton(
label: 'Storage Full',
color: Colors.grey,
onPressed: () async =>
throw Exception('Disk full: insufficient storage'),
),
// 8. Manual Triggers (NEW!)
const Divider(),
const Text(
'Manual Triggers (No Exception Thrown):',
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
),
_ErrorButton(
label: 'Manual Network',
color: Colors.blueAccent,
onPressed: () =>
ErrorHandler.network(message: "Manual WiFi Error Check"),
),
_ErrorButton(
label: 'Manual Auth',
color: Colors.deepOrange,
onPressed: () =>
ErrorHandler.auth(message: "Manual Session Expiry"),
),
_ErrorButton(
label: 'Manual Server',
color: Colors.redAccent,
onPressed: () => ErrorHandler.server(
message: "Manual 503 Service Unavailable",
),
),
const Divider(),
_ErrorButton(
label: 'Sync Build Error (Red Screen)',
color: Colors.black,
onPressed: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => const BuggyWidget(),
),
);
},
),
],
),
),
),
),
);
}
}
class _ErrorButton extends StatelessWidget {
final String label;
final Color color;
final VoidCallback onPressed;
const _ErrorButton({
required this.label,
required this.color,
required this.onPressed,
});
@override
Widget build(BuildContext context) {
return ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: color,
foregroundColor: Colors.white,
),
onPressed: onPressed,
child: Text(label),
);
}
}
class BuggyWidget extends StatelessWidget {
const BuggyWidget({super.key});
@override
Widget build(BuildContext context) {
throw Exception("This is a test BUILD exception!");
}
}