zo_json_to_dart

Generates a strongly-typed Dart model class from a sample JSON file. Supports different output modes: standard (fromJson/toJson/copyWith), freezed, and json_serializablein Beta.

🚀 Getting Started

1. Add Dependency

Add the package to your pubspec.yaml under dev_dependencies:

dev_dependencies:
  zo_json_to_dart: ^0.1.0

🛠️ Usage (Step-by-Step)

You can copy, paste, and edit the commands below to fit your needs.

Step 1: Prepare your JSON file

Create a sample JSON file (e.g., user.json) anywhere in your project:

Step 2: Run the generation command

Copy and edit the command below. Replace <input.json> and <output> with your actual paths.

dart run zo_json_to_dart -i <input.json> -o <output> -n <ClassName> -t <type>

Common Examples to Copy & Paste:

1. Standard Mode (Default) Generates a standard Dart class with fromJson, toJson, and copyWith methods. No extra dependencies required.

dart run zo_json_to_dart -i user.json -o lib/models -n User -t standard

2. Freezed Mode Generates an annotated base file for the freezed package. (Requires freezed_annotation, freezed, and build_runner)

dart run zo_json_to_dart -i user.json -o lib/models -n User -t freezed

3. JSON Serializable Mode Generates an annotated base file for the json_serializable package. (Requires json_annotation, json_serializable, and build_runner)

dart run zo_json_to_dart -i user.json -o lib/models -n User -t json_serializable

Step 3: Run Build Runner (If using freezed or json_serializable)

If you generated a file using -t freezed or -t json_serializable, you must run build_runner to generate the .freezed.dart or .g.dart files.

dart run build_runner build --delete-conflicting-outputs

(If using Flutter, run flutter pub run build_runner build --delete-conflicting-outputs)

🚩 Command Flags Reference

  • -i, --input (required) — path to the source JSON file.
  • -o, --output (required) — where to write the generated .dart file.
    • Folder: e.g., -o lib/models (creates the file inside this folder named automatically).
    • Exact file: e.g., -o lib/models/user.dart.
  • -n, --name (optional) — root class name to use (e.g., User). If omitted, it's derived from the input file's name.
  • -t, --type (optional) — type of generation: standard (default), freezed, or json_serializable.
  • -h, --help — print usage.

Feel free to post a feature requests or report a bug issues.

My Other packages

  • zo_animated_border: A package that provides a modern way to create gradient borders with animation in Flutter

  • zo_micro_interactions: A curated set of high-quality Flutter micro-interactions designed for modern, polished apps.

  • zo_screenshot: The zo_screenshot plugin helps restrict screenshots and screen recording in Flutter apps, enhancing security and privacy by preventing unauthorized screen captures.

  • zo_collection_animation: A lightweight Flutter package to create smooth collect animations for coins carts

  • connectivity_watcher: A Flutter package to monitor internet connectivity with subsecond response times, even on mobile networks.

  • ultimate_extension: Enhances Dart collections and objects with utilities for advanced data manipulation and simpler coding.

  • theme_manager_plus: Allows customization of your app's theme with your own theme class, eliminating the need for traditional

  • date_util_plus: A powerful Dart API designed to augment and simplify date and time handling in your Dart projects.

  • pick_color: A Flutter package that allows you to extract colors and hex codes from images with a simple touch.

Libraries

zo_json_to_dart
Public API for programmatic use. Most consumers just want the CLI (dart run zo_json_to_dart) — see the README.