has_is_getters 1.1.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 61

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. That leads to cleaner code on the caller side:

Before                                 |  After
                                       |
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: ^1.1.0

dev_dependencies:
  build_runner: ^1.7.1
  has_is_getters_generator: ^1.1.0

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 #

For is-getters, you can also opt-in to using the field name as a prefix:

class Image {
  @GenerateIsGetters(usePrefix: true)
  Color background;
}
image.isBackgroundBlue

Private fields #

The getters can also be generated for private fields.

[1.1.0] - 2019-11-13

  • Add support for private fields.

[1.0.0] - 2019-11-07

  • Version bump as Dart 2.6.0 got released, so extension methods are now supported.
  • Apply new Dart version to environment constraints.

[0.0.2] - 2019-11-04

  • Add readme.
  • Clean up code.
  • Add licenses.
  • Add tooling folder for possible future linting support.

[0.0.1] - 2019-11-04

  • Initial release supporting generation of has-getters and is-getters.

example/main.dart

// ignore_for_file: undefined_getter, uri_has_not_been_generated

import 'package:has_is_getters/has_is_getters.dart';

part 'main.g.dart';

enum Shape { round, curved }

class Fruit {
  @GenerateHasGetter(generateNegation: true)
  int number;

  @GenerateIsGetters(usePrefix: true, generateNegations: true)
  Shape theShape;

  Fruit(this.number, this.theShape);
}

void main() {
  var banana = Fruit(2, Shape.curved);

  print(banana.isTheShapeRound);
  print(banana.isTheShapeNotRound);
  print(banana.hasNumber);
  print(banana.hasNoNumber);

  if (banana.theShape == Shape.round) {
    print('The shape is round.');
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  has_is_getters: ^1.1.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:has_is_getters/has_is_getters.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
22
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
61
Learn more about scoring.

We analyzed this package on Dec 9, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.6.1
  • pana: 0.13.1+4

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.6.0 <3.0.0
build_config ^0.4.1 0.4.1+1
Transitive dependencies
charcode 1.1.2
checked_yaml 1.0.2
collection 1.14.12
json_annotation 3.0.0
meta 1.1.8
path 1.6.4
pub_semver 1.4.2
pubspec_parse 0.1.5
source_span 1.5.5
string_scanner 1.0.5
term_glyph 1.1.0
yaml 2.2.0
Dev dependencies
test ^1.9.3