0 purchases
value container
ValueContainer #
ValueContainer is a package that will help you to separate values and validate each value on its own
Usage #
Use ValueContainer class on its own
This validator will always return Value must be true error message if value is not true
final container = ValueContainer<bool, String>(
name: 'AlwaysTrue',
value: false,
validator: (value) => value == true ? null : 'Value must be true',
);
print(container);
copied to clipboard
results:
AlwaysTrue(value: false, isValid: false, error: Value must be true)
copied to clipboard
Extend ValueContainer #
You can create your own value container too
class EmailAddress extends ValueContainer<String, String> {
EmailAddress([String? email])
: super(
name: 'EmailAddress',
value: email,
validator: validator,
);
static String? validator(String? value) {
if (value == null || value.isEmpty) return 'Email address is required';
if (!value.contains('@')) return 'Email address must contain @';
return null;
}
}
copied to clipboard
now lets create an invalid instance of it
final emailAddress = EmailAddress('my_email_address');
print(emailAddress);
copied to clipboard
results:
EmailAddress(value: my_email_address, isValid: false, error: Email address must contain @)
copied to clipboard
now lets crate a valid instance of it
final emailAddress = EmailAddress('[email protected]');
print(emailAddress);
copied to clipboard
results:
EmailAddress(value: [email protected], isValid: true, error: null)
copied to clipboard
Typed Errors ValueContainer #
Lets define an Abstract Class called PasswordError and use it as error type
class PasswordError {
final String message;
const PasswordError(this.message);
@override
toString() => 'PasswordError($message)';
}
copied to clipboard
Lets create our error classes
class RequiredPasswordError extends PasswordError {
const RequiredPasswordError() : super('Password is required');
}
class MinimumLengthPasswordError extends PasswordError {
const MinimumLengthPasswordError({int length = 6})
: super('Password must be at least $length characters');
}
copied to clipboard
lets create our custom ValueContainer
class Password extends ValueContainer<String, PasswordError> {
Password([String? password])
: super(
name: 'Password',
value: password,
validator: validator,
);
static PasswordError? validator(String? value) {
if (value == null || value.isEmpty) return RequiredPasswordError();
if (value.length < 8) return MinimumLengthPasswordError(length: 8);
return null;
}
}
copied to clipboard
Now lets use our custom value container
final pass = Password();
print(pass);
final pass2 = Password('123456');
print(pass2);
final pass3 = Password('12345678');
print(pass3);
if (pass.hasErrorOf<RequiredPasswordError>()) {
print('Pass: Password is required');
}
// or //
pass2.onErrorOf<MinimumLengthPasswordError>((password, error) {
print('pass2: Password is too short');
});
copied to clipboard
results:
Password(value: null, isValid: false, error: PasswordError(Password is required))
Password(value: 123456, isValid: false, error: PasswordError(Password must be at least 8 characters))
Password(value: 12345678, isValid: true, error: null)
Pass: Password is required
pass2: Password is too short
copied to clipboard
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.