nameof_modern 0.0.1 copy "nameof_modern: ^0.0.1" to clipboard
nameof_modern: ^0.0.1 copied to clipboard

An upgrade modern code of the official and original nameof. Nameof package, that is Generator for Dart class member's names, such as fields etc...

Star on Github Pub Points License: MIT


Nameof Modern #

In some cases, there are projects that need to access entity names for different cases in programming, such as methods, properties, constructors, etc. Unfortunately, today Flutter does not have a reflection mechanism designed for this purpose. But there is code generation! This package uses code generation to help access the property names of a class, method, etc. This package is an update of the original package: nameof. Nameof Modern is one hundred percent nameof code. Credits go to the creators of nameof ❤️. Nameof Modern is updated and has all the latest updates for the proper functioning of the new 2024 and future projects. This package is new and with the help of the community we are improving it ❤️.

🗒️Important: This package is created based on the package nameof. nameof is a name generator for Dart class members, such as fields, properties, methods, and constructors. nameof_modern is one hundred percent nameof code, credits to nameof author ❤️.

  • This is the official documentation guide for nameof. For more source information, please visit the official page of nameof

This guide is based on the official nameof documentation and has been updated for the modern version of nameof_modern.

Motivation #

Sometimes there is a need to access the names of programming language entities, such as methods, properties, constructors, etc. Unfortunately, Flutter does not have a reflection mechanism designed for this purpose. But there is code generation! It is all you need for access to names of code entities with this package.

Menu #

How to use #

Install #

To use Nameof Modern, you will need your typical build_runner code-generator setup.
First, install build_runner and Nameof Modern by adding them to your pubspec.yaml file:

If you are using creating a Flutter project:

$ flutter pub add nameof_annotation_modern
$ flutter pub add --dev build_runner
$ flutter pub add --dev nameof_modern

If you are using creating a Dart project:

$ dart pub add nameof_annotation_modern
$ dart pub add --dev build_runner
$ dart pub add --dev nameof_modern

This installs three packages:

Run the generator #

To run the code generator, execute the following command:

dart run build_runner build

For Flutter projects, you can also run:

flutter pub run build_runner build

Note that like most code-generators, nameof_modern will need you to both import the annotation (nameof_annotation_modern) and use the part keyword on the top of your files.

As such, a file that wants to use nameof_modern will start with:

import 'package:nameof_annotation_modern/nameof_annotation_modern.dart';

part 'my_file.nameof.dart';

Using Nameof #

Simple usage #

For example we have a class Movie. For names generation of this class you need to tell generator some instructions with nameof modern annotation:

@nameof
class Movie {
  final String title;
  final String description;
  final int year;
  final String coverUrl;

  Movie(this.title, this.description, this.year, this.coverUrl);
}

Then you need to run generator Run the generator

It will generate next code:

/// Container for names of elements belonging to the [Movie] class
abstract class NameofMovie {
  static const String className = 'Movie';

  static const String constructor = '';

  static const String fieldTitle = 'title';
  static const String fieldDescription = 'description';
  static const String fieldYear = 'year';
  static const String fieldCoverUrl = 'coverUrl';
}

Then use it in your code:

print(NameofMovie.fieldTitle);
print(NameofMovie.fieldDescription);
print(NameofMovie.fieldYear);
print(NameofMovie.fieldCoverUrl);

It is simple!

Also you may to use nameof annotation for abstract classes and mixins.

Models coverage #

You can have very precision setting of coverage of model's members with use coverage settings and @NameofIgnore annotation. For example two next configurations will lead to one output.

  • First configuration:
@Nameof(coverage: Coverage.excludeImplicit)
class Movie {
  final String title;
  final String description;
  @nameofKey
  final int year;
  @nameofKey
  final String coverUrl;

  Movie(this.title, this.description, this.year, this.coverUrl);
}
  • Second configuration:
@Nameof(coverage: Coverage.includeImplicit)
class Movie {
  @nameofIgnore
  final String title;
  @nameofIgnore
  final String description;

  final int year;

  final String coverUrl;
  
  @nameofIgnore
  Movie(this.title, this.description, this.year, this.coverUrl);
}

Output:

/// Container for names of elements belonging to the [Movie] class
abstract class NameofMovie {
  static const String className = 'Movie';

  static const String fieldYear = 'year';
  static const String fieldCoverUrl = 'coverUrl';
}

Take an attention for coverage setting, @nameofKey and @nameofIgnore annotations. If you do not set coverage, generator will use includeImplicit setting by default.

Override names #

If you want override name of element you can do it! Code:

@nameof
class Ephemeral {
  @NameofKey(name: 'AbRaCadabra')
  String get flushLight => 'Purple';
}

Generator output:

/// Container for names of elements belonging to the [Ephemeral] class
abstract class NameofEphemeral {
  static const String className = 'Ephemeral';

  static const String constructor = '';

  static const String propertyGetFlushLight = 'AbRaCadabra';
}

As can you see property was renamed. Output has AbRaCadabra not flushLight.

NameofKey targets #

@NameofKey annotatition applyed for public fields, methods, properties and constructors.

Configurations #

Nameof offers various options to customize the generated code. For example, you may want to change coverage behaviour of model.

To do so, there are two possibilities:

Changing the behavior for a specific model #

If you want to customize the generated code for only one specific class, you can do so by using annotation setting:

@Nameof(coverage: Coverage.excludeImplicit)
class Empoyee {...}

Changing the behavior for the entire project #

Instead of applying your modification to a single class, you may want to apply it to all Nameof models at the same time.

You can do so by customizing a file called build.yaml
This file is an optional configuration file that should be placed next to your pubspec.yaml:

project_folder/
  pubspec.yaml
  build.yaml
  lib/

There, you will be able to change the same options as the options found in @Nameof (see above) by writing:

targets:
  $default:
    builders:
      nameof:
        options:
          coverage: includeImplicit
          

Two settings for coverage is available: includeImplicit (default) and excludeImplicit

Contributing #

Contributions are welcome!

Here is a curated list of how you can help:

  • Report bugs and scenarios that are difficult to implement
  • Report parts of the documentation that are unclear
  • Fix typos/grammar mistakes
  • Update the documentation or add examples
  • Implement new features by making a pull-request
4
likes
130
points
64
downloads

Publisher

verified publisherelssiany.com

Weekly Downloads

An upgrade modern code of the official and original nameof. Nameof package, that is Generator for Dart class member's names, such as fields etc...

Homepage
Repository (GitHub)
View/report issues
Contributing

Documentation

API reference

License

MIT (license)

Dependencies

analyzer, build, build_config, nameof_annotation_modern, source_gen

More

Packages that depend on nameof_modern