write method

  1. @override
Future<String?> write(
  1. String key,
  2. dynamic value
)
override

Writes a value to secure storage.

The value is converted to a string before storage, and that string is returned. If value is null, the key is deleted and null is returned.

Example:

// Store a string
await storage.write('user.name', 'John Doe');

// Store a number
await storage.write('app.version', 2.1);

// Store JSON
await storage.write('user.preferences', jsonEncode(prefs));

// Delete by writing null
await storage.write('temp.key', null);

See also:

  • read - Read a stored value
  • delete - Delete a stored value

Implementation

@override
Future<String?> write(String key, dynamic value) async {
  if (value == null) {
    await delete(key);
    return null;
  }
  final text = value.toString();
  await _store.write(key: key, value: text);
  return text;
}