Skip to content

Instantly share code, notes, and snippets.

@ethaizone
Last active May 26, 2024 08:04
Show Gist options
  • Save ethaizone/e41b3ff13564c92bdf28ca43b53b3b22 to your computer and use it in GitHub Desktop.
Save ethaizone/e41b3ff13564c92bdf28ca43b53b3b22 to your computer and use it in GitHub Desktop.
Dart: Note collections
// Example about using extension on Enum.
enum ValidationError { empty }
// Magical happen in here but it's on this enum only. Not in prototype.
extension ValidationErrorMessage on ValidationError {
String text(String label) {
switch (this) {
case ValidationError.empty:
return 'Please enter a password';
}
}
}
// Example validate function
ValidationError? validate(value) {
if (value == null) return ValidationError.empty;
return null;
}
// Validate then return error message
var errorMessage = validate(null)?.text();
print('Error message: $errorMessage');
// FYI. If someone found issue as method doesn't exists on other library.
// Run `flutter clean`, reopen IDE and run `flutter pub get` again.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment