Skip to content

flutter-cavalry/next_available_name

Repository files navigation

next_available_name

pub package Build Status

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', 'liu'];
var maxAttempts = 100;
for (var name in nextNames) {
  var assigned = await nextAvailableName(name, maxAttempts, (name) async {
    if (!namesTaken.contains(name)) {
      // Don't forget to add new names to the tracking set.
      namesTaken.add(name);
      return true;
    }
    return false;
  });
  print('$name -> $assigned');
}
/**
  * Prints:
  * zheng -> zheng (2)
  * wang -> wang
  * liu -> liu (3)
  * liu -> liu (4)
  */

Releases

No releases published

Packages

No packages published

Languages