next_available_name 1.0.0 next_available_name: ^1.0.0 copied to clipboard
Get next available name.
Get next available name.
Usage #
///
/// Gets next available name.
///
/// [maxAttempts] if no available name found after [maxAttempts] attempts, null is returned.
/// [availableFn] used to check if a name is available.
Future<String?> nextAvailableName(
String name,
int maxAttempts,
Future<bool> Function(String name) availableFn
);
Example #
// Use a set to track names that have been taken.
Set<String> namesTaken = {'liu', 'zheng', 'liu (2)'};
var nextNames = ['zheng', 'wang', 'liu'];
var maxAttempts = 100;
for (var name in nextNames) {
var assigned = await nextAvailableName(
name, maxAttempts, (name) async => !namesTaken.contains(name));
print('$name -> $assigned');
}
/**
* Prints:
* zheng -> zheng (2)
* wang -> wang
* liu -> liu (3)
*/