asset_gen

Pub Version License: MIT

A Dart CLI tool to automatically generate a Dart file containing constants for all assets in your Flutter project.


โœจ Features

  • ๐Ÿ” Recursively scans your assets/ directory
  • ๐Ÿงพ Generates a lib/generated/assets.dart file
  • ๐Ÿ”’ Avoids hardcoded strings and typos
  • ๐Ÿค– Great for code completion in IDEs
  • โœ… Simple to use โ€” just one command!

๐Ÿš€ Getting Started

1. โœ… Activate the package globally:

dart pub global activate asset_gen

2. ๐Ÿ“ Project structure (example):

my_flutter_app/
โ”œโ”€โ”€ assets/
โ”‚   โ”œโ”€โ”€ images/
โ”‚   โ”‚   โ””โ”€โ”€ logo.png
โ”‚   โ””โ”€โ”€ icons/
โ”‚       โ””โ”€โ”€ user.png
โ”œโ”€โ”€ lib/

3. โ–ถ๏ธ Run the generator:

asset_gen

This will generate the file:

lib/generated/assets.dart

๐Ÿ“ฆ Output Example

Generated lib/generated/assets.dart:

// GENERATED CODE - DO NOT MODIFY BY HAND

class AppAssets {
  static const String images_logo_png = 'assets/images/logo.png';
  static const String icons_user_png = 'assets/icons/user.png';
}

๐Ÿ›  Usage in Code

import 'generated/assets.dart';

Image.asset(AppAssets.images_logo_png);

โš™๏ธ Command Options

You can optionally specify a custom asset folder:

asset_gen my_custom_assets/

This will scan my_custom_assets/ instead of the default assets/ folder.


๐Ÿ’ก Why Use asset_gen?

  • Prevent errors from typos in asset file paths
  • Enable autocomplete for assets in your IDE
  • Cleaner code โ€” no more 'assets/.../file.png' strings
  • Easy to regenerate when assets are added/removed
  • Fits right into your build or CI pipeline

๐Ÿ“„ License

Licensed under the MIT License. See the LICENSE file for details.


๐Ÿ™‹ Contributing

Contributions, issues, and feature requests are welcome!

  1. Fork this repo
  2. Create your feature branch: git checkout -b feature/awesome-feature
  3. Commit your changes: git commit -m 'Add awesome feature'
  4. Push to the branch: git push origin feature/awesome-feature
  5. Open a pull request


โค๏ธ Built for Flutter Developers

Save time, reduce bugs, and make your Flutter code cleaner with asset_gen.

Libraries

asset_gen