vm_class_gen 3.5.0+11 copy "vm_class_gen: ^3.5.0+11" to clipboard
vm_class_gen: ^3.5.0+11 copied to clipboard

Generator for `VmClass`.

VmClassGen #

The builder generate code when they find members annotated with classes defined in vm_class_annotation.

A Dart library for creating enhanced object representations, with capabilities like custom toString, automated property management, and more. Ideal for developers looking to streamline their object model logic.

Overview #

VmClassGen provides a robust architecture for handling Dart objects, extending functionality with a focus on string representation, equality checks, and easy serialization.

Features #

  • Custom toString Implementations: Generate detailed or short string representations automatically.
  • Enhanced Equality Checks: Built on top of Equatable for robust comparison.
  • Flexible Code Generation: Use annotations to automate boilerplate code creation.

Quick Start #

Usage #

Generate property-centric classes with the VmClassGen annotation:

import 'package:vm_class/vm_class.dart';

part 'example.g.dart';

@VmClassGen()
class Address extends VmClass {
  const Address({
    required this.street,
    required this.city,
  });

  final String street;
  final String city;

  @override
  String? get shortQualifier => '$city, $street';

  @override
  Map<String, dynamic> get toStringProps => _$AddressStringProps(this);
}

void main() {
  final address = Address(street: '123 Main St', city: 'Metropolis');
  print(address.toString());
}

Building creates the corresponding part example.g.dart:

Map<String, dynamic> _$AddressStringProps(Address instance) => {
      'street': instance.street,
      'city': instance.city,
    };

Ignoring Fields #

You can exclude specific fields from being included in the toStringProps map by using the @VmClassGenField.ignore() annotation. This is useful for fields that should not appear in the generated string representation.

Example #

import 'package:vm_class/vm_class.dart';
import 'package:vm_class_annotation/vm_class_annotation.dart';

part 'example.g.dart';

@VmClassGen()
class Example extends VmClass {
  const Example({
    required this.name,
    this.secret,
  });

  final String name;

  @VmClassGenField.ignore()
  final String? secret;

  @override
  Map<String, dynamic> get toStringProps => _$ExampleStringProps(this);
}

In this example, the secret field will not be included in the toStringProps map.

Generated code:

Map<String, dynamic> _$ExampleStringProps(Example instance) => {
      'name': instance.name,
    };

For actual result, see vm_class.