Skip to content

Instantly share code, notes, and snippets.

@ethaizone
Last active May 26, 2024 08:04
Show Gist options
  • Select an option

  • Save ethaizone/e41b3ff13564c92bdf28ca43b53b3b22 to your computer and use it in GitHub Desktop.

Select an option

Save ethaizone/e41b3ff13564c92bdf28ca43b53b3b22 to your computer and use it in GitHub Desktop.

Revisions

  1. ethaizone revised this gist May 26, 2024. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions extension_on_enum.dart
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    enum ValidationError { empty }

    // Magical happen in here but it's on this enum only. Not in prototype.
    extension on ValidationError {
    extension ValidationErrorMessage on ValidationError {
    String text(String label) {
    switch (this) {
    case ValidationError.empty:
    @@ -21,4 +21,7 @@ ValidationError? validate(value) {
    // Validate then return error message
    var errorMessage = validate(null)?.text();

    print('Error message: $errorMessage');
    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.
  2. ethaizone revised this gist May 20, 2024. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions extension_on_enum.dart
    Original file line number Diff line number Diff line change
    @@ -13,12 +13,12 @@ extension on ValidationError {
    }

    // Example validate function
    ValidationError validate(value) {
    ValidationError? validate(value) {
    if (value == null) return ValidationError.empty;
    return null;
    }

    // Validate then return error message
    var errorMessage = validate(null)?.text()
    var errorMessage = validate(null)?.text();

    print('Error message: $errorMessage');
  3. ethaizone created this gist May 20, 2024.
    24 changes: 24 additions & 0 deletions extension_on_enum.dart
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    // Example about using extension on Enum.

    enum ValidationError { empty }

    // Magical happen in here but it's on this enum only. Not in prototype.
    extension 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');