import 'dart:convert'; import 'package:shared_preferences/shared_preferences.dart'; import '../models/project.dart'; class StorageService { static const String _projectsKey = 'projects'; static const String _currentPortKey = 'current_port'; Future saveProjects(List projects) async { try { final prefs = await SharedPreferences.getInstance(); final jsonList = projects.map((p) => p.toJson()).toList(); final jsonString = json.encode(jsonList); final success = await prefs.setString(_projectsKey, jsonString); if (!success) { throw Exception('Failed to save projects to storage'); } // Force a commit on web await prefs.reload(); } catch (e) { rethrow; } } Future> loadProjects() async { try { final prefs = await SharedPreferences.getInstance(); // Reload to ensure we get the latest data await prefs.reload(); final jsonString = prefs.getString(_projectsKey); if (jsonString == null || jsonString.isEmpty) { return []; } final List jsonList = json.decode(jsonString); return jsonList.map((json) => Project.fromJson(json)).toList(); } catch (e) { return []; } } Future saveCurrentPort(int port) async { final prefs = await SharedPreferences.getInstance(); await prefs.setInt(_currentPortKey, port); } Future loadCurrentPort() async { final prefs = await SharedPreferences.getInstance(); return prefs.getInt(_currentPortKey) ?? 8888; } }