setStorageItem function

Future<void> setStorageItem({
  1. required String key,
  2. dynamic value,
})

Sets the value of a storage item with the specified key.

The key parameter is required and represents the key of the storage item. The value parameter is dynamic and represents the value to be stored. The value is encoded as JSON before being stored in SharedPreferences.

Throws an Exception if an error occurs while setting the storage item.

Implementation

Future<void> setStorageItem({required String key, dynamic value}) async {
  try {
    final prefs = await SharedPreferences.getInstance();
    String jsonValue = json.encode(value);
    await prefs.setString(key, jsonValue);
  } catch (e) {
    throw Exception('setItem error: $e');
  }
}