forDisplayName static method
Returns the persisted UUID for displayName, minting a fresh one
when none is stored yet. Whitespace in displayName is trimmed
and the lookup is case-sensitive — pass the same canonical form
(typically lowercased) you intend to use across sessions.
Provide prefs when you already hold the SharedPreferences
instance; otherwise it's resolved internally via
SharedPreferences.getInstance().
Implementation
static Future<String> forDisplayName(
String displayName, {
SharedPreferences? prefs,
String keyPrefix = defaultKeyPrefix,
}) async {
final canonical = displayName.trim();
if (canonical.isEmpty) {
throw ArgumentError('displayName cannot be empty');
}
final store = prefs ?? await SharedPreferences.getInstance();
final key = '$keyPrefix$canonical';
final existing = store.getString(key);
if (existing != null && existing.isNotEmpty) return existing;
final fresh = _uuid.v4();
await store.setString(key, fresh);
return fresh;
}