From b5b1475d5da32b07e249c32af3a25b187faf4fe8 Mon Sep 17 00:00:00 2001 From: Dustin Date: Sun, 2 Aug 2026 17:00:23 +0200 Subject: [PATCH] =?UTF-8?q?v2.42.1=20=E2=80=94=20HIGH:=20Timer-Periodic-Le?= =?UTF-8?q?ak=20behoben?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - MeloApp von StatelessWidget zu StatefulWidget konvertiert - _autoScanTimer als globale Variable gespeichert - Timer wird in _MeloAppState.dispose() gecancelt --- lib/main.dart | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index 8074a05..d3d7555 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -21,6 +21,9 @@ import 'screens/login_screen.dart'; final FlutterLocalNotificationsPlugin notificationsPlugin = FlutterLocalNotificationsPlugin(); +/// Globaler Timer für periodischen Auto-Scan (wird in MeloAppState.dispose gecancelt) +Timer? _autoScanTimer; + void main() async { WidgetsFlutterBinding.ensureInitialized(); @@ -96,7 +99,7 @@ void main() async { runApp(const MeloApp()); } -/// Führt Musik-Scan beim Start aus + Timer.periodic alle 3 Stunden (Issue #6) +/// Führt Musik-Scan beim Start aus + startet Timer.periodic alle 3 Stunden (Issue #6) void _starteAutoScan() { // Sofort beim Start scannen (asynchron, blockiert nicht) MusikScanner().scanneMusikOrdner().then((songs) { @@ -105,8 +108,8 @@ void _starteAutoScan() { MeloLogger().fehler('auto_scan_start', e); }); - // Periodischer Scan alle 3 Stunden - Timer.periodic(const Duration(hours: 3), (_) { + // Periodischer Scan alle 3 Stunden – Timer wird in MeloAppState.dispose() gecancelt + _autoScanTimer = Timer.periodic(const Duration(hours: 3), (_) { MusikScanner().scanneMusikOrdner().then((songs) { MeloLogger().zustand('auto_scan_periodisch', {'gefunden': songs.length}); }).catchError((e) { @@ -172,9 +175,14 @@ Future _onServiceStart(ServiceInstance service) async { return true; } -class MeloApp extends StatelessWidget { +class MeloApp extends StatefulWidget { const MeloApp({super.key}); + @override + State createState() => _MeloAppState(); +} + +class _MeloAppState extends State { @override Widget build(BuildContext context) { final auth = AuthService(); @@ -188,4 +196,11 @@ class MeloApp extends StatelessWidget { : const LoginScreen(), ); } + + @override + void dispose() { + _autoScanTimer?.cancel(); + _autoScanTimer = null; + super.dispose(); + } }