搜尋分類、套件與程式碼儲存庫

套件詳情v0.1.2

data_class

使用 pkg:macros 在 Dart 中實驗性支援資料類別

近 30 天下載
51
喜歡數
60
Pub 分數
50

安裝套件

flutter pub add data_class

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

data_class 套件透過巨集為 Dart 提供資料類別的實驗性支援,讓定義不可變資料結構更加簡潔且無需冗餘程式碼。它會自動產生所需的 toString、hashCode 及 operator == 方法,簡化主要儲存資料之類別的建立。這提升了程式碼可讀性,並減少手動實作所導致的錯誤。專為尋求在 Dart 應用中採用更乾淨資料模型方法的開發者設計,該套件與現代 Dart 工具鏈整合,並支援 JSON 序列化工作流程。

適用情境

  • 定義不可變的資料模型
  • 減少資料類別中的樣板程式碼
  • 簡化 JSON 序列化
  • 建立乾淨的領域模型
  • 簡化狀態管理
  • 提升程式碼可維護性

主要特色

  • 自動生成 `toString`、`hashCode` 和 `==`
  • 對不可變資料類別的支援
  • 與 Dart 宏的整合
  • 最小化樣板程式碼
  • 內建的 JSON 序列化支援
  • 乾淨、易讀的類別定義

適合對象

  • 使用現代 Dart 版本的 Dart 開發人員
  • 需要乾淨資料建模的專案
  • 使用不可變資料的狀態管理
  • JSON-heavy applications
  • 注重程式碼清晰性和可維護性的團隊
  • 原型設計或快速開發

使用建議

  • 實驗狀態可能影響穩定性
  • 需要支援巨集的 Dart
  • 生態系統採用有限
  • 可能需要隨著 Dart SDK 的變更而更新
  • 不適用於舊版 Dart 項目
  • 巨集使用的潛在學習曲線

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

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

所屬分類

依賴

依賴清單 3 項

原始碼儲存庫

使用此套件的專案

暫無相關資料。