Search categories, packages and repositories

Package detailsv0.1.2

data_class

Experimental support for data classes in Dart using pkg:macros

30-day downloads
51
Likes
60
Pub points
50

Install package

flutter pub add data_class

Package Overview

Package selection guidance compiled from public package information, community data, and project usage.

The data_class package provides experimental support for data classes in Dart through the use of macros, enabling concise and boilerplate-free definitions of immutable data structures. It simplifies the creation of classes that primarily hold data by automatically generating required methods like `toString`, `hashCode`, and `operator ==`. This enhances code readability and reduces errors from manual implementation. Designed for developers seeking a cleaner approach to data modeling in Dart applications, it integrates with modern Dart tooling and supports JSON serialization workflows.

Use cases

  • Defining immutable data models
  • Reducing boilerplate in data classes
  • Simplifying JSON serialization
  • Creating clean domain models
  • Streamlining state management
  • Enhancing code maintainability

Key features

  • Automatic generation of `toString`, `hashCode`, and `==`
  • Support for immutable data classes
  • Integration with Dart macros
  • Minimal boilerplate
  • Built-in JSON serialization support
  • Clean, readable class definitions

Suitable for

  • Dart developers using modern Dart versions
  • Projects requiring clean data modeling
  • State management with immutable data
  • JSON-heavy applications
  • Teams prioritizing code clarity and maintainability
  • Prototyping or rapid development

Considerations

  • Experimental status may affect stability
  • Requires Dart with macro support
  • Limited ecosystem adoption
  • May require updates with Dart SDK changes
  • Not suitable for legacy Dart projects
  • Potential learning curve for macro usage

Original README

Original README snapshot from the linked source repository. Visit the repository for the latest content.

data_class

build pub package License: MIT

Support for data classes in Dart using macros.

✨ Features

🪨 const constructors with required, named parameters

🖨️ copyWith with optional, nullable, named parameters

✨ toString for an improved string representation

☯️ operator== and hashCode for value equality

🧑‍💻 Example

import 'package:data_class/data_class.dart';

@Data()
class Person {
  final String name;
}

void main() {
  // 🪨 Create a const instance with required, name parameters.
  const dash = Person(name: 'Dash');

  // 🖨️ Create copies of your object.
  final sparky = dash.copyWith(name: 'Sparky');

  // ✨ Human-readable string representation.
  print(dash); // Person(name: Dash)
  print(sparky); // Person(name: Sparky)

  // ☯️ Value equality comparisons.
  print(dash == dash.copyWith()); // true
  print(dash == sparky); // false
}

🚀 Quick Start

  1. Add package:data_class to your pubspec.yaml

    dependencies:
      data_class: any
    
  2. Enable experimental macros in analysis_options.yaml

    analyzer:
      enable-experiment:
        - macros
    
  3. Use the @Data annotation (see above example).

  4. Run it

    dart --enable-experiment=macros run main.dart
    

*Requires Dart SDK >= 3.5.0

Categories

Dependencies

Dependency list 3 items

Source repository

Projects using this package

No data available yet.