82 lines
2.9 KiB
Dart
82 lines
2.9 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:connectivity_plus/connectivity_plus.dart';
|
|
import 'dart:io' if (dart.library.html) 'dart:html' as io;
|
|
|
|
class ErrorHandler {
|
|
/// Zeigt eine benutzerfreundliche Fehlermeldung
|
|
static void showError(BuildContext context, dynamic error) {
|
|
String message = 'Ein Fehler ist aufgetreten';
|
|
final errorString = error.toString().toLowerCase();
|
|
|
|
// Web-kompatible Fehlerbehandlung (ohne direkte Typ-Prüfungen für dart:io)
|
|
if (error is String) {
|
|
message = error;
|
|
} else if (errorString.contains('failed host lookup') ||
|
|
errorString.contains('socketexception') ||
|
|
errorString.contains('network') ||
|
|
errorString.contains('connection')) {
|
|
message = 'Keine Internetverbindung. Bitte prüfe deine Verbindung.';
|
|
} else if (errorString.contains('timeout') ||
|
|
errorString.contains('timed out')) {
|
|
message = 'Zeitüberschreitung. Bitte versuche es erneut.';
|
|
} else if (error is FormatException) {
|
|
message = 'Datenfehler. Bitte versuche es erneut.';
|
|
} else if (errorString.contains('404') || errorString.contains('not found')) {
|
|
message = 'Nicht gefunden.';
|
|
} else if (errorString.contains('401') ||
|
|
errorString.contains('403') ||
|
|
errorString.contains('unauthorized') ||
|
|
errorString.contains('forbidden')) {
|
|
message = 'Nicht autorisiert. Bitte melde dich an.';
|
|
} else if (errorString.contains('500') ||
|
|
errorString.contains('server error') ||
|
|
errorString.contains('internal server')) {
|
|
message = 'Server-Fehler. Bitte versuche es später erneut.';
|
|
}
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text(message),
|
|
backgroundColor: Colors.red,
|
|
action: SnackBarAction(
|
|
label: 'OK',
|
|
textColor: Colors.white,
|
|
onPressed: () {},
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
/// Prüft die Internetverbindung
|
|
static Future<bool> checkConnectivity() async {
|
|
try {
|
|
final connectivityResult = await Connectivity().checkConnectivity();
|
|
return connectivityResult != ConnectivityResult.none;
|
|
} catch (e) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// Zeigt einen Retry-Dialog
|
|
static Future<bool> showRetryDialog(BuildContext context, String message) async {
|
|
return await showDialog<bool>(
|
|
context: context,
|
|
builder: (context) => AlertDialog(
|
|
title: const Text('Fehler'),
|
|
content: Text(message),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.of(context).pop(false),
|
|
child: const Text('Abbrechen'),
|
|
),
|
|
ElevatedButton(
|
|
onPressed: () => Navigator.of(context).pop(true),
|
|
child: const Text('Erneut versuchen'),
|
|
),
|
|
],
|
|
),
|
|
) ?? false;
|
|
}
|
|
}
|
|
|