import 'package:flutter/foundation.dart'; import '../../../core/api_gateway.dart'; import 'health_models.dart'; class HealthController extends ChangeNotifier { HealthController(this.gateway); final ApiGateway gateway; JsonMap? stats; String? error; bool busy = false; bool _disposed = false; Future load() async { if (busy || _disposed) return; busy = true; error = null; _notify(); try { final reply = await gateway.call('server_stats'); if (reply['error'] case final String message when message.isNotEmpty) { throw StateError(message); } if (reply['server_stats'] is! Map) { throw StateError( 'Server health information is unavailable on this server.', ); } stats = healthMap(reply['server_stats']); } catch (exception) { error = exception is StateError ? exception.message.toString() : '$exception'; } finally { busy = false; _notify(); } } void _notify() { if (!_disposed) notifyListeners(); } @override void dispose() { _disposed = true; super.dispose(); } }