Last active
May 26, 2024 08:04
-
-
Save ethaizone/e41b3ff13564c92bdf28ca43b53b3b22 to your computer and use it in GitHub Desktop.
Dart: Note collections
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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