Unique class

Asynchronously validates that a value is unique by hitting a backend endpoint.

The default resolver issues GET {endpoint}?{field}={value} and treats a {"unique": true} (or {"available": true}) response body as a pass. Network errors are logged and treated as passes so they never block form submission; the authoritative check still happens on the server.

Future<void> submit() async {
  await Validator.make(data, {
    'slug': [Required(), Unique('/validate/unique', field: 'slug')],
  }).validateAsync();
}

Debounce

Rapid-fire calls (typing into a field with autovalidate) are coalesced via a per-instance debounce window. Only the last call within the window actually reaches the resolver; earlier calls resolve to true (stale) and never record errors. Set debounce to Duration.zero to disable.

Custom Resolver

Override the HTTP call shape with via:

Unique('/validate/unique', field: 'slug').via((endpoint, field, value) async {
  final response = await Http.post(endpoint, data: {field: value});
  return response['unique'] == true;
});
Inheritance

Constructors

Unique(String endpoint, {required String field, Duration debounce = const Duration(milliseconds: 400)})

Properties

debounce Duration
Debounce window. Duration.zero disables debouncing.
final
endpoint String
The endpoint to query.
final
field String
The field name reported to the endpoint.
final
hashCode int
The hash code for this object.
no setterinherited
name String
The short name this rule is known by in a messages override map.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

message() String
Get the validation error message.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
params() Map<String, dynamic>
Get the replacement parameters for the message.
inherited
passes(String attribute, dynamic value, Map<String, dynamic> data) bool
Synchronous fallback. Async rules pass sync validation unconditionally so the async pass can report the real outcome.
inherited
passesAsync(String attribute, dynamic value, Map<String, dynamic> data) Future<bool>
Asynchronously determine if the rule passes.
override
toString() String
A string representation of this object.
inherited
via(UniqueResolver resolver) Unique
Swap the default HTTP resolver for a custom one.

Operators

operator ==(Object other) bool
The equality operator.
inherited