Skip to content
pubscout

dart_mappable

4.10.0, released 2 months ago by schultek.dev

JSON serialization and data classes generator supporting generics, inheritance, and customization.

WASM-readyRock solid
$ flutter pub add dart_mappable
Source repository

What it does

Generates JSON serialization, equality operators, hashCode, toString, and copyWith methods for Dart classes and enums. It excels at handling complex use cases such as polymorphism, type inheritance, and generics.

AI-written summary of version 4.10.0, based on the package's README and metadata. Report an inaccuracy

Use it when

  • You need robust JSON serialization that handles complex class inheritance and generics.
  • You want built-in data class features like copyWith, equality, and toString alongside JSON mapping.

Skip it when

  • You want zero build-runner or code generation setup in your project.
  • You are already heavily invested in json_serializable or freezed and don't require dart_mappable's advanced generic and inheritance features.

Strengths

  • Full support for generics, polymorphism, and inheritance.
  • All-in-one package generating serialization, equality, toString, and copyWith.
  • Highly flexible with mapping hooks and custom configuration options.

Weaknesses

  • Requires running build_runner for code generation.
  • toJson() returns a String rather than a Map<String, dynamic>, which requires attention when migrating from other packages.

Setup

Kind
Pure Dart
Effort
Low
Code generation
Yes (build_runner)
Direct dependencies
3

Add dart_mappable as a dependency and dart_mappable_builder and build_runner as dev_dependencies, then run dart run build_runner build.

import 'package:dart_mappable/dart_mappable.dart';

part 'model.mapper.dart';

@MappableClass()
class MyClass with MyClassMappable {
  final int myValue;
  MyClass(this.myValue);
}

Alternatives

Packages that do the same job and could replace dart_mappable.

  • json_serializable

    If you prefer standard Map-based toJson returns and standard JSON annotation configurations.

  • freezed

    If you want union types and pattern matching features for immutable data models.

Works with

Packages commonly used together with dart_mappable: ones it builds on, ones built on it, and its own family.