generateKey static method
String
generateKey({
- String? name,
- int? timeMills,
- int extraKeySize = 5,
- KeyFormats format = KeyFormats.numbers,
Generates a key based on the provided name or the current timestamp.
Parameters:
name
: The name to generate a key from. If provided, the key is generated by converting the name to lowercase.timeMills
: The timestamp to generate a key from. If not provided, the current timestamp is used.extraKeySize
: The size of the extra key to append. Default is 5.
Example:
String key = KeyGenerator.generateKey(name: 'MyImage');
print(key); // Output: 'myimage'
String timestampKey = KeyGenerator.generateKey(timeMills: 1644613725000);
print(timestampKey); // Output: '1644613725000xxxxx' (xxxxx is a random string of 5 characters)
Implementation
static String generateKey({
String? name,
int? timeMills,
int extraKeySize = 5,
KeyFormats format = KeyFormats.numbers,
}) {
if (name != null) {
return Replacement.auto(name).toLowerCase();
} else {
final ms = timeMills ?? DateTime.timestamp().millisecondsSinceEpoch;
final extra = RandomProvider.getString(
data: format.value,
max: extraKeySize,
);
final key = "$ms$extra";
return key;
}
}