Create dart data classes easily, fast and without writing boilerplate or running code generation.

Overview

Dart Data Class Generator

GitHub Stars Downloads

Create dart data classes easily, fast and without writing boilerplate or running code generation.

Features

The generator can generate the constructor, copyWith, toMap, fromMap, toJson, fromJson, toString, operator == and hashCode methods for a class based on class properties or raw JSON.

Additionally the generator has a couple of useful quickfixes to speed up your development process. See the Additional Features Section for more.

If this extension is helpful to you, consider giving it a star on GitHub or leave a review on the Visual Studio Marketplace ❤️

Create Data Classes Based on Class Properties

Usage

You can generate data classes either by the quick fix dialog or by running a command. In the quick fix dialog you have the option to not only generate whole data classes but also only specific methods. The command has the advantage of being able to generate multiple classes at the same time.

Quick fix

  • Create a class with properties.
  • Place your cursor on the first line of the class, the constructor or a field.
  • Hit CTRL + . to open the quick fix dialog.
  • Choose one of the available options.

Command

  • Create a class with properties.
  • Hit CTRL + P to open the command dialog.
  • Search for Dart Data Class Generator: Generate from class properties and hit enter.
  • When there are multiple classes in the current file, choose the ones you'd like to create data classes of in the dialog.

It is also possible to run the generator on an existing data class (e.g. when some parameters changed). The generator will then try to find the changes and replace the class with its updated version. Note that custom changes to generated functions may be overriden.

You can also customize the generator for example to use Equatable for value equality. See the Settings section for more options.

Enums

In order for enums to be correctly serialized from and to JSON, please annotate them using a comment like so:

// enum
final Enum myEnum;

Usage with Equatable

Although using the generator is fast, it still doesn't spare you from all the boiler plate necessary, which can be visually distracting. To reduce the amount of boiler plate needed, the generator works with Equatable. Just extend the class with Equatable or mix with EquatableMixin and the generator will use Equatable for value equality.

You can also use the setting dart_data_class_generator.useEquatable, if you always want to use Equatable for value equality.

Create Data Classes Based on JSON (Beta)

Usage

  • Create an empty dart file.
  • Paste the raw JSON without modifying it into the otherwise empty file.
  • Hit CTRL + P to open the command dialog.
  • Search for Dart Data Class Generator: Generate from JSON and hit enter.
  • Type in a class name in the input dialog. This will be the name of the top level class if the JSON contains nested objects, all other class names will be infered from the JSON keys.
  • When there are nested objects in the JSON, a dialog will be appear if you want to seperate the classes into multiple files or if all classes should be in the same file.

Note:
This feature is still in beta!
Many API's return numbers like 0 or 1 as an integer and not as a double even when they otherwise are. Thus the generator may confuse a value that is usually a double as an int.

Additional Features

The extension includes some additional quick fixes that might be useful to you:

Import refactoring

Sort imports alphabetically and bring them into the correct format easily.

Settings

You can customize the generator to only generate the functions you want in your settings file.

  • dart_data_class_generator.quick_fixes: If true, enables quick fixes to quickly generate data classes or specific methods only.
  • dart_data_class_generator.useEquatable: If true, uses Equatable for value equality and hashCode.
  • dart_data_class_generator.fromMap.default_values: If true, checks if a field is null when deserializing and provides a non-null default value.
  • dart_data_class_generator.constructor.default_values: If true, generates default values for the constructor.
  • dart_data_class_generator.constructor.required: If true, generates @required annotation for every constructor parameter. Note: The generator wont generate default values for the constructor if enabled!
  • dart_data_class_generator.json.seperate: Whether to seperate a JSON into multiple files, when the JSON contains nested objects. ask: choose manually every time, seperate: always seperate into multiple files, current_file: always insert all classes into the current file.
  • dart_data_class_generator.override.manual: If true, asks, when overriding a class (running the command on an existing class), for every single function/constructor that needs to be changed whether the generator should override the function or not. This allows you to preserve custom changes you made to the function/constructor that would be otherwise overwritten by the generator.
  • dart_data_class_generator.constructor.enabled: If true, generates a constructor for a data class.
  • dart_data_class_generator.copyWith.enabled: If true, generates a copyWith function for a data class.
  • dart_data_class_generator.toMap.enabled: If true, generates a toMap function for a data class.
  • dart_data_class_generator.fromMap.enabled: If true, generates a fromMap function for a data class.
  • dart_data_class_generator.toJson.enabled: If true, generates a toJson function for a data class.
  • dart_data_class_generator.fromJson.enabled: If true, generates a fromJson function for a data class.
  • dart_data_class_generator.toString.enabled: If true, generates a toString function for a data class.
  • dart_data_class_generator.equality.enabled: If true, generates an override of the == (equals) operator for a data class.
  • dart_data_class_generator.hashCode.enabled: If true, generates a hashCode function for a data class.
  • dart_data_class_generator.hashCode.use_jenkins: If true, uses the Jenkins SMI hash function instead of bitwise operator from dart:ui.
You might also like...

A collection of bricks to be more productive when writing code.

A collection of bricks to be more productive when writing code.

A collection of bricks that enable developers to be more productive when writing code. Bricks 🧱 Brick Description Version clean_architecture_global_s

Nov 12, 2022

This is a command-line app written on dart language for flutter applications that will help you to generate some boilerplate code

dart-generator Manual installation: 1- generate a platform executable from code dart compile exe main.dart -o generator this will generate a new gene

Oct 26, 2022

Code generator for Flutter's theme extension classes.

Code generator for Flutter's theme extension classes.

Welcome to Theme Tailor, a code generator and theming utility for supercharging Flutter ThemeExtension classes introduced in Flutter 3.0! The generato

Jan 2, 2023

Data Migrator - provide a universal translator for data by being portable, diverse, and efficient in migrating and converting data across discrete schemas

Data Migrator - provide a universal translator for data by being portable, diverse, and efficient in migrating and converting data across discrete schemas

Data Migrator - provide a universal translator for data by being portable, diverse, and efficient in migrating and converting data across discrete schemas

Jan 2, 2023

A Flutter application running on mobile, web and desktop. Stay tuned to see who will win the stars race!

A Flutter application running on mobile, web and desktop. Stay tuned to see who will win the stars race!

The live stars race Access this project running at jhbitencourt.github.io/stars-race An app running on mobile, web and desktop. How long do you think

Jul 20, 2022

Toast Library for Flutter, Easily create toast messages in single line of code

Toast Library for Flutter, Easily create toast messages in single line of code

Create Toast messages on a Flutter Event. fluttertoast: ^8.0.8 Toast Library for Flutter, Easily create Personalised toast messages in single line of

Feb 14, 2022

Form builder fix - A package helps in creation of data collection forms in Flutter by removing the boilerplate needed to build a form

Flutter FormBuilder - flutter_form_builder This package helps in creation of dat

Jan 29, 2022

Working proof of the Go (golang) server running inside Flutter

Working proof of the Go (golang) server running inside Flutter

flap Working proof of the Go server running inside Flutter Video in action Prerequisites Flutter 2.0 Go 1.16 Build Go server cd go macOS: make maco

Dec 17, 2022

Proof of concept of Flutter running on CarPlay

Proof of concept of Flutter running on CarPlay

CarPlay Flutter Widgets What is this? This is a proof of concept of a Flutter based app running on Apple CarPlay app. What can I use it for? You can u

Apr 9, 2022
Releases(0.3.0)
Owner
null
This repo is a boilerplate to create flutter application easily. It is based on **GetX**.

flutter-app boilerplate This repo is a boilerplate to create flutter application easily. It is based on GetX. More info about GetX here. The app has b

NEWWAVE SOLUTIONS JSC 35 Dec 3, 2022
Flutter boilerplate - A boilerplate project created in flutter using MobX and Provider

Boilerplate Project A boilerplate project created in flutter using MobX and Prov

Wali Khan 0 Jan 22, 2022
Flutter ui boilerplate is easiest way to create new flutter project with clean code and well organized file folder.

Flutter UI Boilerplate "Sharing for fun" Flutter ui boilerplate is easiest way to create new flutter project with clean code and well organized file f

Dimas Ibnu Malik 122 Dec 1, 2022
This is an opinionated code-generation tool from GraphQL to Dart/Flutter.

GraphQL Codegen This is an opinionated code-generation tool from GraphQL to Dart/Flutter. It'll allow you to generate Dart serializers and client help

United Traders 1 Dec 29, 2021
Automatic source code generation for Dart

Overview source_gen provides utilities for automated source code generation for Dart: A framework for writing Builders that consume and produce Dart c

Dart 418 Dec 30, 2022
A Flutter mobile application built completely using DhiWise and Supabase without coding single line of code. With 100% system generated code

Flutter Expension Getting Started with Flutter ?? Generated with ❤️ from Dhiwise A Flutter mobile application built completely using DhiWise and Supab

DhiWise 11 Oct 23, 2022
A flutter plugin about qr code or bar code scan , it can scan from file、url、memory and camera qr code or bar code .Welcome to feedback your issue.

r_scan A flutter plugin about qr code or bar code scan , it can scan from file、url、memory and camera qr code or bar code .Welcome to feedback your iss

PengHui Li 112 Nov 11, 2022
Buildpack dart - A server app built using Shelf, configured to enable running with Docker

Hosted at https://dart-buildpack-demo-ruyjilv5wq-uc.a.run.app/ Buildpack defined

Kevin Moore 12 Dec 15, 2022
Flutter shareable package of object-oriented classes for local caching of user data in json

json_cache Json Cache is an object-oriented package to serve as a layer on top of local storage packages - packages that persist data locally on the u

Dartoos 10 Dec 19, 2022