import 'package:flutter/foundation.dart'; import '../models/user.dart'; import '../services/auth_service.dart'; class UserProvider extends ChangeNotifier { final AuthService _authService = AuthService(); User? _user; bool _isLoading = false; User? get user => _user; bool get isLoggedIn => _user != null; bool get isLoading => _isLoading; UserProvider() { _loadSavedUser(); } Future _loadSavedUser() async { _isLoading = true; notifyListeners(); try { _user = await _authService.getSavedUser(); } catch (e) { print('Fehler beim Laden des Benutzers: $e'); } finally { _isLoading = false; notifyListeners(); } } Future login(String username, String password) async { _isLoading = true; notifyListeners(); try { final user = await _authService.login(username, password); if (user != null) { _user = user; _isLoading = false; notifyListeners(); return true; } } catch (e) { print('Login-Fehler: $e'); } _isLoading = false; notifyListeners(); return false; } Future logout() async { await _authService.logout(); _user = null; notifyListeners(); } Future refreshUser() async { await _loadSavedUser(); } }