has_is_getters 0.0.2 copy "has_is_getters: ^0.0.2" to clipboard
has_is_getters: ^0.0.2 copied to clipboard

discontinued
outdated

Annotations for generating getters for checking whether a field is null or checking the value of a field.

This library lets you annotate fields of classes to automatically generate getters for checking whether a field is null or, in case of enum fields, checking the value of a field.

if (fruit.price != null) {             |  if (fruit.hasPrice) {
  ...                                  |    ...
}                                      |  }
if (fruit.color == Color.blue          |  if (fruit.isBlue || fruit.isNotOrange) {
    || fruit.color != Color.orange) {  |    ...
  ...                                  |  }
}                                      |

Here's how to get started:

1. 📦 Add these packages to your dependencies:

dependencies:
  has_is_getters: ^0.0.2

dev_dependencies:
  build_runner: ^1.7.1
  has_is_getters_generator: ^0.0.2

2. 📎 Annotate fields with @GenerateHasGetter() or enum fields with @GenerateIsGetters().

import 'package:has_is_getters/has_is_getters.dart';

part 'my_file.g.dart';

enum Color { orange, blue }

class Fruit {
  @GenerateHasGetter()
  int price;

  @GenerateIsGetters()
  Color color;
}

3. 🏭 Run pub run build_runner build in the command line (or flutter pub run build_runner build, if you're using Flutter). The getters will automatically be generated, so you can do stuff like this:

fruit.hasPrice // equal to: fruit.price != null
fruit.isOrange  // equal to: fruit.color == Color.orange

Negations #

You can opt-in to generating negations by customizing the annotations:

class Paper {
  @GenerateHasGetter(generateNegation: true)
  @GenerateIsGetters(generateNegations: true)
  Color color;
}
paper.hasNoColor
paper.isNotOrange

Prefixes #

You can also opt-in to using the field name as a prefix for the getters.

class Image {
  @GenerateIsGetters(usePrefix: true)
  Color background;
}
image.isBackgroundBlue
2
likes
30
pub points
0%
popularity

Publisher

unverified uploader

Annotations for generating getters for checking whether a field is null or checking the value of a field.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

build_config

More

Packages that depend on has_is_getters