embed
1.7.1, released 8 weeks ago by norelease.dev
Generates Dart code that embeds arbitrary file contents as literals.
What it does
The package reads non‑Dart files at build time and creates Dart constants (strings, binary, or structured records) containing their contents. It updates the generated code whenever the source files change.
AI-written summary of version 1.7.1, based on the package's README and metadata. Report an inaccuracy
Use it when
- You need compile‑time access to configuration files, test fixtures, or other assets without runtime I/O.
- You want type‑safe access to embedded structured data like YAML, JSON, or TOML.
Skip it when
- You require dynamic loading of files at runtime.
- The project targets platforms where build_runner is unavailable or undesirable.
Strengths
- Eliminates runtime file I/O and async handling
- Supports multiple formats (text, binary, YAML, JSON, TOML)
- Works with Dart records for structured data
Weaknesses
- Requires build_runner and code generation step
- Only works for compile‑time known files
- Limited to Dart‑only projects
Setup
- Kind
- Pure Dart
- Effort
- Medium
- Code generation
- Yes (build_runner)
- Direct dependencies
- 10
Add embed_annotation, embed, and build_runner as dev dependencies; run build_runner to generate code.
import 'package:embed_annotation/embed_annotation.dart';
part 'example.g.dart';
@EmbedLiteral('assets/config.yaml')
const config = _$config;Alternatives
Packages that do the same job and could replace embed.
- freezed
Code generation for immutable classes, tagged unions, and cloning without boilerplate.
96Compare - copy_with_extension
Annotation for generating copyWith extensions code using copy_with_extension_gen.
87Compare - copy_with_extension_gen
Generate type‑safe copyWith extensions for annotated classes and record typedefs.
85Compare - super_annotations
Generate Dart code using custom annotations defined alongside your source files.
68Compare
Works with
Packages commonly used together with embed: ones it builds on, ones built on it, and its own family.
- analyzerembed depends on itPerforms static analysis of Dart code for tool integration and embedding.
- yamlembed depends on itA Dart library for parsing YAML documents into Dart data structures.
- buildembed depends on itPackage for authoring build_runner compatible code generators.
- source_genembed depends on itSource code generation builders and utilities for the Dart build system.
- recaseembed depends on itChanges the case of input text to various conventions.
- tomlembed depends on itParse and encode TOML configuration files in Dart.
- build_runnerUsed togetherA build system for Dart code generation and modular compilation.