Skip to main content

analyzer_kit_annotation 2.0.0 copy "analyzer_kit_annotation: ^2.0.0" to clipboard
analyzer_kit_annotation: ^2.0.0 copied to clipboard

Annotations for use with the analyzer_kit analyzer plugin.

Analyzer Kit Annotation #

Annotations for use with analyzer_kit — a Dart Analyzer plugin that provides lint rules and quick fixes for common boilerplate code generation patterns.

Getting Started #

Add the annotation package to your dependencies:

dependencies:
  analyzer_kit_annotation: ^2.0.0

Then enable the plugin in your analysis_options.yaml (all rules are enabled by default):

plugins:
  analyzer_kit: ^2.0.0

Annotations #

Annotation Convenience Constant Generated Code
CompositeAnnotation([...]) — Bundles any combination of the annotations below
@CopyWith() @copyWith copyWith method
@OverrideEquality() @overrideEquality == operator and hashCode getter
@OverrideToString() @overrideToString toString method
@Serialize() @serialize Serialization method (default: toMap)
@Deserialize() @deserialize Deserialization factory (default: fromMap)
@Ignore() @ignore Excludes a field from generated methods
@Redact() @redact Masks a sensitive field's value in generated output

Each feature annotation has a convenience const for concise usage (e.g., @copyWith is equivalent to @CopyWith()).

Usage #

CompositeAnnotation — Reusable Bundles #

CompositeAnnotation lets you group any combination of annotations (including custom configurations) into a reusable const:

import 'package:analyzer_kit_annotation/analyzer_kit_annotation.dart';

const dataClass = CompositeAnnotation([
  CopyWith(),
  OverrideEquality(),
  OverrideToString(),
  Serialize(),
  Deserialize(),
]);

@dataClass
class User {
  final String name;
  final int age;

  User({required this.name, required this.age});

  // IDE quick fix generates: copyWith, toString, hashCode, ==, toMap, fromMap
}

You can define specialized bundles for different layers of your app:

const apiModel = CompositeAnnotation([
  CopyWith(),
  OverrideEquality(deepCollectionEquality: false),
  Serialize(name: .toJson()),
  Deserialize(name: .fromJson()),
]);

@apiModel
class Point {
  final double x;
  final double y;

  Point({required this.x, required this.y});
}

@CopyWith #

Generates a copyWith method that creates a new instance with selected fields overridden:

@copyWith
class Settings {
  final bool darkMode;
  final int fontSize;

  Settings({required this.darkMode, required this.fontSize});

  // Generated:
  // Settings copyWith({bool? darkMode, int? fontSize}) =>
  //     Settings(darkMode: darkMode ?? this.darkMode, fontSize: fontSize ?? this.fontSize);
}

@OverrideEquality #

Generates == operator and hashCode overrides based on all generatable fields:

@overrideEquality
class Coordinate {
  final double lat;
  final double lng;

  Coordinate({required this.lat, required this.lng});

  // Generated: operator == and hashCode using lat, lng
}

Deep Collection Equality

For classes with collection fields (List, Set, Map), the generated equality uses deepEquals and deepHash from this package by default, which perform recursive element-wise comparison. You can opt out to use shallow Dart-native comparisons:

@OverrideEquality(deepCollectionEquality: false)
class Inventory {
  final List<String> items;

  Inventory({required this.items});

  // Generated: uses Object.hashAll/indexed.every instead of deepHash/deepEquals
}

@Ignore #

Excludes a field (or primary constructor declaring parameter) from code generation:

@dataClass
class Account {
  final String id;

  @ignore // Ignored in all generated methods
  final String? cachedLabel;

  @Ignore({.overrideToString, .serialize}) // Ignored only in toString & toMap
  final String? token;

  Account({required this.id, this.cachedLabel, this.token});
}

@Redact #

Masks a sensitive field's value with a placeholder in generated output while keeping the field in the class and all other generated methods (copyWith, ==, hashCode, fromMap):

@dataClass
class Account {
  final String id;

  @redact // Renders '[REDACTED]' in toString()
  final String password;

  @Redact({.overrideToString, .serialize}, '***') // Renders '***' in toString() & toMap()
  final String apiKey;

  Account({required this.id, required this.password, required this.apiKey});
}

@OverrideToString #

Generates a toString override listing all generatable fields:

@overrideToString
class Config {
  final String host;
  final int port;

  Config({required this.host, required this.port});

  // Generated:
  // @override
  // String toString() => 'Config(host: $host, port: $port)';
}

@Serialize #

Generates a serialization method that converts the instance to a Map<String, dynamic>:

// Default: generates toMap()
@serialize
class Event {
  final String title;
  final DateTime date;

  Event({required this.title, required this.date});

  // Generated: Map<String, dynamic> toMap() => {'title': title, 'date': date};
}

// Custom method name and field casing
@Serialize(name: .toJson(), fieldRename: .snake)
class ApiResponse {
  final int statusCode;

  ApiResponse({required this.statusCode});

  // Generated: Map<String, dynamic> toJson() => {'status_code': statusCode};
}

// Fully custom name
@Serialize(name: .custom('serialize'))
class Document {
  final String content;

  Document({required this.content});

  // Generated: Map<String, dynamic> serialize() => {'content': content};
}

@Deserialize #

Generates a factory constructor that creates an instance from a Map<String, dynamic>:

// Default: generates fromMap factory
@deserialize
class User {
  final String name;
  final int age;

  User({required this.name, required this.age});

  // Generated:
  // factory User.fromMap(Map<String, dynamic> map) {
  //   return User(name: map['name'] as String, age: map['age'] as int);
  // }
}

// Custom method name
@Deserialize(name: .fromJson())
class ApiPayload {
  final String data;

  ApiPayload({required this.data});

  // Generated: factory ApiPayload.fromJson(Map<String, dynamic> map) { ... }
}

Runtime Helpers #

This package also provides two runtime utility functions used by generated equality code:

Function Description
deepHash(Object?) Computes a hash using deep recursive collection equality
deepEquals(Object?, Object?) Compares two objects using deep recursive collection equality

These functions are powered by DeepCollectionEquality() from package:collection and handle nested collections (List<List<int>>, Map<String, List<int>>, etc.) correctly.

1
likes
160
points
35
downloads

Documentation

API reference

Publisher

verified publishermaranix.in

Weekly Downloads

Annotations for use with the analyzer_kit analyzer plugin.

Repository (GitHub)
View/report issues

Topics

#annotation #analyzer #codegen

License

MIT (license)

Dependencies

collection

More

Packages that depend on analyzer_kit_annotation