import 'package:shared_preferences/shared_preferences.dart'; const _legacyActiveCollectionPrefKey = 'active_collection_id'; String activeCollectionPrefKeyForUser(String userId) { return 'active_collection_id_$userId'; } Future readActiveCollectionId( SharedPreferences prefs, { required String userId, }) async { final scopedKey = activeCollectionPrefKeyForUser(userId); final scopedValue = prefs.getString(scopedKey); if (scopedValue != null && scopedValue.isNotEmpty) { return scopedValue; } final legacyValue = prefs.getString(_legacyActiveCollectionPrefKey); if (legacyValue == null || legacyValue.isEmpty) { return null; } await prefs.setString(scopedKey, legacyValue); await prefs.remove(_legacyActiveCollectionPrefKey); return legacyValue; } Future writeActiveCollectionId( SharedPreferences prefs, { required String userId, required String collectionId, }) { return prefs.setString(activeCollectionPrefKeyForUser(userId), collectionId); } Future clearActiveCollectionId( SharedPreferences prefs, { required String userId, }) async { await prefs.remove(activeCollectionPrefKeyForUser(userId)); await prefs.remove(_legacyActiveCollectionPrefKey); }