import 'dart:convert'; import 'package:hive/hive.dart'; import 'package:shared_preferences/shared_preferences.dart'; import '../models/user.dart'; class StorageService { static final StorageService _instance = StorageService._internal(); factory StorageService() => _instance; StorageService._internal(); Box? _box; SharedPreferences? _prefs; Future init() async { _box = Hive.box('zhinianBox'); _prefs = await SharedPreferences.getInstance(); } Future saveUser(User user) async { await _box?.put('user', jsonEncode(user.toJson())); } User? getUser() { final data = _box?.get('user'); if (data == null) return null; try { return User.fromJson(jsonDecode(data)); } catch (_) { return null; } } Future clearUser() async { await _box?.delete('user'); } Future setString(String key, String value) async { await _prefs?.setString(key, value); } String? getString(String key) { return _prefs?.getString(key); } Future setBool(String key, bool value) async { await _prefs?.setBool(key, value); } bool? getBool(String key) { return _prefs?.getBool(key); } Future remove(String key) async { await _prefs?.remove(key); } } final storageService = StorageService();