VmClass

A base class for value objects in Dart that provides structured string representations, and deep equality checks. This package extends Equatable to simplify object comparison and debugging.

Features

  • Automatic toString Representation: Generates structured output for better debugging.
  • Deep Equality Comparison: Implements Equatable for efficient equality checks.
  • Shortened Representation: Allows compact toString output with shortQualifier.

Installation

Add vm_class to your pubspec.yaml:

dependencies:
  vm_class: latest_version

Then, run:

dart pub get

Usage

Defining a Value Object

Create a class extending VmClass and override toStringProps:

import 'package:vm_class/vm_class.dart';

class Address extends VmClass {
  final String street;
  final String city;

  const Address({required this.street, required this.city});

  @override
  Map<String, dynamic> get toStringProps => {
    'street': street,
    'city': city,
  };

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

Using VmClass

Define a User class with nested VmClass instances:

class User extends VmClass {
  final String name;
  final int age;
  final Address address;
  final List<String> hobbies;
  final List<User> friends;

  const User({
    required this.name,
    required this.age,
    required this.address,
    required this.hobbies,
    required this.friends,
  });

  @override
  Map<String, dynamic> get toStringProps => {
    'name': name,
    'age': age,
    'address': address,
    'hobbies': hobbies,
    'friends': friends,
  };

  @override
  String? get shortQualifier => '$name, $age';
}

Example Usage

void main() {
  final user1 = User(
    name: 'John Doe',
    age: 30,
    address: Address(street: '123 Main St', city: 'Metropolis'),
    hobbies: ['Reading', 'Gaming', 'Hiking'],
    friends: [
      User(
        name: 'Alice Smith',
        age: 28,
        address: Address(street: '456 Elm St', city: 'Smalltown'),
        hobbies: ['Painting', 'Running'],
        friends: [],
      ),
      User(
        name: 'Bob Johnson',
        age: 32,
        address: Address(street: '789 Oak St', city: 'Big City'),
        hobbies: ['Cooking', 'Cycling'],
        friends: [],
      ),
    ],
  );

  final user2 = User(
    name: 'John Doe',
    age: 30,
    address: Address(street: '123 Main St', city: 'Metropolis'),
    hobbies: ['Reading', 'Gaming', 'Hiking'],
    friends: [],
  );

  print('Full Representation:\n$user1');
  print('\nShort Representation:\n${user1.toStringShort()}');
  print('\nFully Short Representation:\n${user2.toStringShort(fully: true)}');

  print('\nEquality Check:');
  print('user1 == user2: ${user1 == user2}');
}

Output:

Full:
User(
	name: John Doe,
	age: 30,
	address: Address(
		street: 123 Main St,
		city: Metropolis,
	),
	hobbies: [
		Reading,
		Gaming,
		Hiking,
	],
	friends: [
		User(
			name: Alice Smith,
			age: 28,
			address: Address(
				street: 456 Elm St,
				city: Smalltown,
			),
			hobbies: [
				Painting,
				Running,
			],
			friends: [],
		),
		User(
			name: Bob Johnson,
			age: 32,
			address: Address(
				street: 789 Oak St,
				city: Big City,
			),
			hobbies: [
				Cooking,
				Cycling,
			],
			friends: [],
		),
	],
)

Short:
User(
	name: John Doe,
	age: 30,
	address: Metropolis, 123 Main St,
	hobbies: [
		Reading,
		Gaming,
		Hiking,
	],
	friends: [
		Alice Smith, 28,
		Bob Johnson, 32,
	],
)

Fully short:
John Doe, 30

Equality Check:
user1 == user2: true

API Reference

  • toStringProps: Defines the properties included in toString().
  • toStringShort({bool fully = false}): Returns a shorter representation.
  • shortQualifier: Customizes shortened object representation.

License

This package is released under the MIT License.

Libraries

vm_class