fassets 0.0.1
fassets: ^0.0.1 copied to clipboard
Generate type-safe Dart constants from Flutter asset directories.
fassets
Generate type-safe Dart constants from your Flutter asset directories.
Stop using magic strings like 'assets/icons/home.svg' — let fassets generate clean, static-const references for every asset in your project.
Features #
- Reads declared asset paths from
pubspec.yaml - Generates one Dart class per asset folder with
static const Stringentries - Respects folder hierarchy —
assets/icons/home/dark/→AppDarkHomeIcons - Incremental — only appends new constants, never removes or reorders
- Barrel file — auto-generates
index.dartwith sorted exports - Selective — use
--folderto regenerate a single subtree - Zero external runtime dependencies in your Flutter project
Installation #
dart pub global activate fassets
After activation, the fassets command is available globally from any directory.
Quick start #
# cd into your Flutter project root
cd my_flutter_app
# Generate constants for all declared asset paths
fassets
# That's it! Import and use:
import 'package:my_flutter_app/src/core/domain/constants/assets/index.dart';
Image.asset(AppIcons.home); // 'assets/icons/home.svg'
Image.asset(AppPng.splashBg); // 'assets/png/splash_bg.png'
Usage #
Run fassets from the root of your Flutter project (where pubspec.yaml lives):
# Generate for all declared asset paths
fassets
# Generate for a specific folder only
fassets -f assets/icons
# Custom output directory
fassets -o lib/constants/assets
# Verbose output
fassets -v
CLI options #
| Flag | Description |
|---|---|
-h, --help |
Print usage information |
--version |
Print the tool version |
-v, --verbose |
Show detailed output |
-f, --folder |
Process only this asset folder (full path from root) |
-o, --output |
Output directory (default: lib/src/core/domain/constants/assets) |
How it works #
- Reads the
flutter → assetssection from yourpubspec.yaml - Scans those directories recursively for files
- Generates one Dart class per folder with
static const Stringentries - Creates or updates a barrel
index.dartthat exports everything
Naming conventions #
| Input | Output |
|---|---|
Folder assets/icons/ |
Class AppIcons |
Folder assets/icons/home/ |
Class AppHomeIcons |
Folder assets/icons/home/dark/ |
Class AppDarkHomeIcons |
File home.svg |
Constant home |
File splash_bg.png |
Constant splashBg |
File delete_icon.svg |
Constant deleteIcon |
Regeneration behaviour #
| Scenario | Behaviour |
|---|---|
| File doesn't exist | Create fresh |
| File exists, no new assets | Touch nothing |
| File exists, new assets added | Append only new constants, leave rest untouched |
Barrel index.dart |
Only add new exports, never duplicate |
--folder rules #
- Must be the full path from project root, e.g.
assets/icons/home - Validated against
pubspec.yaml— if not declared, warns and skips - Only touches files belonging to that folder and its subfolders
- All other existing generated files are completely untouched
Edge cases #
| Situation | Behaviour |
|---|---|
| Folder in pubspec but missing on disk | Warn and skip |
| Folder exists but is empty | Warn and skip |
--folder path not in pubspec |
Warn and skip |
Output structure #
lib/src/core/domain/constants/assets/
├── app_icons.dart
├── app_png.dart
├── app_home_icons.dart
├── app_dark_home_icons.dart
└── index.dart ← barrel file, always up to date
Example #
The example/ directory contains a minimal Flutter project structure you can try:
example/
├── pubspec.yaml
├── assets/
│ ├── icons/
│ │ ├── home.svg
│ │ └── settings.svg
│ └── png/
│ └── splash_bg.png
Run from the example directory:
cd example
dart run ../bin/fassets.dart
Generated output:
app_icons.dart
// GENERATED BY fassets — DO NOT EDIT BY HAND
// ignore_for_file: constant_identifier_names
class AppIcons {
AppIcons._();
static const String baseUrl = 'assets/icons';
static const String home = '$baseUrl/home.svg';
static const String settings = '$baseUrl/settings.svg';
}
app_png.dart
// GENERATED BY fassets — DO NOT EDIT BY HAND
// ignore_for_file: constant_identifier_names
class AppPng {
AppPng._();
static const String baseUrl = 'assets/png';
static const String splashBg = '$baseUrl/splash_bg.png';
}
index.dart
// GENERATED BY fassets — DO NOT EDIT BY HAND
export 'app_icons.dart';
export 'app_png.dart';
☕ Support #
🌐 Connect #
Contributing #
Contributions are welcome! Feel free to open issues and pull requests.
License #
MIT