<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ asset management - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ asset management - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Sun, 26 Jul 2026 04:13:30 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/asset-management/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ How to Manage Assets in Flutter using  flutter_gen ]]>
                </title>
                <description>
                    <![CDATA[ Managing assets like images, icons, and fonts in a Flutter project can quickly become a tedious task, especially as your application grows. Manual referencing is prone to typos, introduces maintenance overhead, and can hinder team collaboration. Fort... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-manage-assets-in-flutter-using-fluttergen/</link>
                <guid isPermaLink="false">69012bf59b2c5393aed5bccd</guid>
                
                    <category>
                        <![CDATA[ Flutter ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Dart ]]>
                    </category>
                
                    <category>
                        <![CDATA[ flutter-aware ]]>
                    </category>
                
                    <category>
                        <![CDATA[ asset management ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Atuoha Anthony ]]>
                </dc:creator>
                <pubDate>Tue, 28 Oct 2025 20:47:49 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/res/hashnode/image/upload/v1761684457969/a8a6b9bc-780f-4e06-bf8a-19b90cd632f4.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Managing assets like images, icons, and fonts in a Flutter project can quickly become a tedious task, especially as your application grows. Manual referencing is prone to typos, introduces maintenance overhead, and can hinder team collaboration.</p>
<p>Fortunately, the <code>flutter_gen</code> package provides an elegant solution by automating asset generation, bringing type safety and a streamlined workflow to your development process.</p>
<p>This comprehensive guide will walk you through setting up a Flutter project with <code>flutter_gen</code>, explaining each step and code block in detail so you can effortlessly integrate this powerful tool into your projects.</p>
<h3 id="heading-table-of-contents">Table of Contents</h3>
<ol>
<li><p><a class="post-section-overview" href="#heading-prerequisites">Prerequisites</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-why-fluttergen-the-advantages-of-automated-asset-management">Why flutter_gen? The Advantages of Automated Asset Management</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-step-by-step-implementation-guide">Step-by-Step Implementation Guide</a></p>
<ul>
<li><p><a class="post-section-overview" href="#heading-1-project-setup">1. Project Setup</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-2-organize-your-assets">2. Organize Your Assets</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-3-run-code-generation">3. Run Code Generation</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-4-explore-the-generated-files">4. Explore the Generated Files</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-5-using-generated-assets-in-your-code">5. Using Generated Assets in Your Code</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-running-your-application">Running Your Application</a></p>
</li>
</ul>
</li>
<li><p><a class="post-section-overview" href="#heading-important-considerations">Important Considerations</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-further-reading">Further Reading</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-conclusion">Conclusion</a></p>
</li>
</ol>
<h2 id="heading-prerequisites">Prerequisites</h2>
<p>Before you begin, make sure you have the following installed:</p>
<ol>
<li><p><strong>Flutter SDK:</strong> you should have the latest stable version of Flutter installed and configured. You can check your installation with <code>flutter --version</code>.</p>
</li>
<li><p><strong>A code editor:</strong> Visual Studio Code with the Flutter extension is highly recommended, but any suitable IDE will work.</p>
</li>
</ol>
<h2 id="heading-why-fluttergen-the-advantages-of-automated-asset-management">Why <code>flutter_gen</code>? The Advantages of Automated Asset Management</h2>
<p><code>flutter_gen</code> offers many benefits that can significantly improve your asset management experience in Flutter:</p>
<ol>
<li><p><strong>Type safety:</strong> This is perhaps the most significant advantage. Instead of fragile string paths, <code>flutter_gen</code> creates strongly-typed classes for each asset type (images, icons, fonts). This eliminates runtime errors caused by typos and provides excellent code completion in your IDE, making asset discovery a breeze.<br> <strong>Reduced errors:</strong> Manual asset path management is a common source of bugs. <code>flutter_gen</code> ensures that your asset references are always accurate and up-to-date, drastically reducing the likelihood of runtime errors related to incorrect paths.</p>
</li>
<li><p><strong>Improved code maintainability:</strong> As your project scales, finding and updating assets can become a nightmare. The generated asset classes serve as a centralized, navigable reference point, making it effortless to locate and modify assets without sifting through countless files.</p>
</li>
<li><p><strong>Enhanced collaboration:</strong> In a team environment, <code>flutter_gen</code> streamlines collaboration. Team members can intuitively discover and use assets through code completion, minimizing communication overhead related to asset paths and ensuring consistency across the codebase.</p>
</li>
</ol>
<h2 id="heading-step-by-step-implementation-guide">Step-by-Step Implementation Guide</h2>
<p>Let's dive into setting up your Flutter project with <code>flutter_gen</code>.</p>
<h3 id="heading-1-project-setup">1. Project Setup</h3>
<h4 id="heading-create-a-new-flutter-project">Create a new Flutter project:</h4>
<p>Start by creating a fresh Flutter project. Open your terminal or command prompt and run:</p>
<pre><code class="lang-bash">flutter create flutter_auto_assets
<span class="hljs-built_in">cd</span> flutter_auto_assets
</code></pre>
<p>This command creates a new Flutter project named <code>flutter_auto_assets</code> and navigates you into its directory.</p>
<h4 id="heading-add-dependencies">Add Dependencies:</h4>
<p>Open the <code>pubspec.yaml</code> file located at the root of your project. This file manages your project's dependencies and assets. Add the <code>flutter_gen</code> and <code>flutter_gen_runner</code> packages, along with <code>build_runner</code>, to your <code>pubspec.yaml</code> as shown below:</p>
<pre><code class="lang-yaml"><span class="hljs-attr">name:</span> <span class="hljs-string">flutter_auto_assets</span>
<span class="hljs-attr">description:</span> <span class="hljs-string">A</span> <span class="hljs-string">flutter</span> <span class="hljs-string">app</span> <span class="hljs-string">demonstrating</span> <span class="hljs-string">asset</span> <span class="hljs-string">auto</span> <span class="hljs-string">generation</span>
<span class="hljs-attr">publish_to:</span> <span class="hljs-string">'none'</span> <span class="hljs-comment"># Remove this line if you wish to publish to pub.dev</span>

<span class="hljs-attr">version:</span> <span class="hljs-number">1.0</span><span class="hljs-number">.0</span><span class="hljs-string">+1</span>

<span class="hljs-attr">environment:</span>
  <span class="hljs-attr">sdk:</span> <span class="hljs-string">^3.8.0</span>

<span class="hljs-attr">dependencies:</span>
  <span class="hljs-attr">flutter:</span>
    <span class="hljs-attr">sdk:</span> <span class="hljs-string">flutter</span>
  <span class="hljs-attr">cupertino_icons:</span> <span class="hljs-string">^1.0.8</span>
  <span class="hljs-attr">flutter_gen:</span> <span class="hljs-string">^5.12.0</span> <span class="hljs-comment"># Add flutter_gen here</span>

<span class="hljs-attr">dev_dependencies:</span>
  <span class="hljs-attr">flutter_test:</span>
    <span class="hljs-attr">sdk:</span> <span class="hljs-string">flutter</span>
  <span class="hljs-attr">build_runner:</span> <span class="hljs-string">^2.4.13</span> <span class="hljs-comment"># Add build_runner here</span>
  <span class="hljs-attr">flutter_gen_runner:</span> <span class="hljs-string">^5.12.0</span> <span class="hljs-comment"># Add flutter_gen_runner here</span>

<span class="hljs-attr">flutter:</span>
  <span class="hljs-attr">uses-material-design:</span> <span class="hljs-literal">true</span>
  <span class="hljs-attr">assets:</span>
    <span class="hljs-bullet">-</span> <span class="hljs-string">assets/</span>
    <span class="hljs-bullet">-</span> <span class="hljs-string">assets/images/</span>
    <span class="hljs-bullet">-</span> <span class="hljs-string">assets/icons/</span>

  <span class="hljs-attr">fonts:</span>
    <span class="hljs-bullet">-</span> <span class="hljs-attr">family:</span> <span class="hljs-string">Roboto</span>
      <span class="hljs-attr">fonts:</span>
        <span class="hljs-bullet">-</span> <span class="hljs-attr">asset:</span> <span class="hljs-string">assets/fonts/Roboto-Regular.ttf</span>
</code></pre>
<p>Explanation of the <code>pubspec.yaml</code> additions:</p>
<ol>
<li><p><code>dependencies</code> section: <code>flutter_gen: ^5.12.0</code>: This is the main package that provides the generated asset classes for your Flutter application.</p>
</li>
<li><p><code>dev_dependencies</code> section:</p>
<ul>
<li><p><code>build_runner: ^2.4.13</code>: <code>build_runner</code> is a powerful package that provides a concrete way of generating files in a Flutter project. <code>flutter_gen_runner</code> uses <code>build_runner</code> to execute its code generation logic.</p>
</li>
<li><p><code>flutter_gen_runner: ^5.12.0</code>: This package contains the actual code generator that scans your <code>pubspec.yaml</code> and asset folders to create the type-safe asset references.</p>
</li>
</ul>
</li>
<li><p><code>flutter</code> section:</p>
<ul>
<li><p><code>assets:</code>: This section is crucial for telling Flutter which directories contain your assets. We've listed <code>assets/</code>, <code>assets/images/</code>, and <code>assets/icons/</code> to ensure all assets within these folders are bundled with your application.</p>
</li>
<li><p><code>fonts:</code>: This section declares your custom fonts. Here, we've registered the <code>Roboto</code> font family, specifying the path to <code>Roboto-Regular.ttf</code>.</p>
</li>
</ul>
</li>
</ol>
<h3 id="heading-2-organize-your-assets">2. Organize Your Assets</h3>
<p>Create the below folder structure within your project's root directory. This organization helps keep your assets tidy and easily discoverable.</p>
<pre><code class="lang-dart">flutter_auto_assets/
├── assets/
│   ├── fonts/
│   │   └── Roboto-Regular.ttf
│   ├── icons/
│   │   └── file_add.png
│   └── images/
│       └── img.png
├── lib/
│   └── main.dart
└── pubspec.yaml
</code></pre>
<p>A few things to note here:</p>
<ul>
<li><p><strong>Configure Fonts:</strong> Place your font files (for example, <code>Roboto-Regular.ttf</code>) inside the <code>assets/fonts/</code> folder.</p>
</li>
<li><p><strong>Configure Icons:</strong> Place your icon files (for example, <code>file_add.png</code>) inside the <code>assets/icons/</code> folder.</p>
</li>
<li><p><strong>Configure Images:</strong> Place your image files (for example, <code>img.png</code>) inside the <code>assets/images/</code> folder.</p>
</li>
</ul>
<h3 id="heading-3-run-code-generation">3. Run Code Generation</h3>
<p>Now it's time to generate the type-safe asset classes. Open your terminal in the project's root directory and execute the following commands:</p>
<pre><code class="lang-bash">flutter pub get
flutter pub run build_runner build
</code></pre>
<p>Here’s what these commands are doing:</p>
<ol>
<li><p><code>flutter pub get</code>: fetches all the packages declared in your <code>pubspec.yaml</code> file, including <code>flutter_gen</code>, <code>build_runner</code>, and <code>flutter_gen_runner</code>.</p>
</li>
<li><p><code>flutter pub run build_runner build</code>: invokes <code>build_runner</code>, which in turn triggers <code>flutter_gen_runner</code>. The runner will scan your <code>pubspec.yaml</code> and the <code>assets/</code> directory, then generate the necessary Dart files containing your type-safe asset references.</p>
</li>
</ol>
<p>After running these commands, you should see a new folder named <code>gen</code> created inside your <code>lib</code> directory. This <code>gen</code> folder will contain <code>assets.gen.dart</code> and <code>fonts.gen.dart</code>.</p>
<h3 id="heading-4-explore-the-generated-files">4. Explore the Generated Files</h3>
<p>Let's take a look at the files <code>flutter_gen</code> creates for you.</p>
<p><code>fonts.gen.dart</code>: This file contains the auto-generated font family class, providing a type-safe way to reference your custom fonts.</p>
<pre><code class="lang-dart"><span class="hljs-comment">/// <span class="markdown">GENERATED CODE - DO NOT MODIFY BY HAND</span></span>
<span class="hljs-comment">/// <span class="markdown"><span class="hljs-strong">****</span><span class="hljs-strong">****</span><span class="hljs-strong">****</span><span class="hljs-strong">****</span><span class="hljs-strong">****</span><span class="hljs-strong">****</span><span class="hljs-strong">****</span><span class="hljs-strong">****</span><span class="hljs-strong">****</span><span class="hljs-strong">****</span><span class="hljs-strong">****</span><span class="hljs-strong">****</span><span class="hljs-strong">****</span><span class="hljs-emphasis">*</span></span></span>
<span class="hljs-comment">///  <span class="markdown"><span class="hljs-emphasis">FlutterGen</span></span></span>
<span class="hljs-comment">/// <span class="markdown"><span class="hljs-emphasis"><span class="hljs-strong">****</span><span class="hljs-strong">****</span><span class="hljs-strong">****</span><span class="hljs-strong">****</span><span class="hljs-strong">****</span><span class="hljs-strong">****</span><span class="hljs-strong">****</span><span class="hljs-strong">****</span><span class="hljs-strong">****</span><span class="hljs-strong">****</span><span class="hljs-strong">****</span><span class="hljs-strong">****</span><span class="hljs-strong">****</span>*</span></span></span>

<span class="hljs-comment">// coverage:ignore-file</span>
<span class="hljs-comment">// ignore_for_file: type=lint</span>
<span class="hljs-comment">// ignore_for_file: directives_ordering,unnecessary_import,implicit_dynamic_list_literal,deprecated_member_use</span>

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">FontFamily</span> </span>{
  FontFamily._();

  <span class="hljs-keyword">static</span> <span class="hljs-keyword">const</span> <span class="hljs-built_in">String</span> roboto = <span class="hljs-string">'Roboto'</span>;
}
</code></pre>
<p>Here, a <code>FontFamily</code> class is generated and for each font family declared in your <code>pubspec.yaml</code> (for example, <code>Roboto</code>), and a static constant string field is created (for example, <code>roboto</code>). This allows you to reference your font family like <code>FontFamily.roboto</code>, ensuring correctness.</p>
<p><code>assets.gen.dart</code>: This file contains the auto-generated classes for your image and icon assets.</p>
<pre><code class="lang-dart"><span class="hljs-comment">/// <span class="markdown">GENERATED CODE - DO NOT MODIFY BY HAND</span></span>
<span class="hljs-comment">/// <span class="markdown"><span class="hljs-strong">****</span><span class="hljs-strong">****</span><span class="hljs-strong">****</span><span class="hljs-strong">****</span><span class="hljs-strong">****</span><span class="hljs-strong">****</span><span class="hljs-strong">****</span><span class="hljs-strong">****</span><span class="hljs-strong">****</span><span class="hljs-strong">****</span><span class="hljs-strong">****</span><span class="hljs-strong">****</span><span class="hljs-strong">****</span><span class="hljs-emphasis">*</span></span></span>
<span class="hljs-comment">///  <span class="markdown"><span class="hljs-emphasis">FlutterGen</span></span></span>
<span class="hljs-comment">/// <span class="markdown"><span class="hljs-emphasis"><span class="hljs-strong">****</span><span class="hljs-strong">****</span><span class="hljs-strong">****</span><span class="hljs-strong">****</span><span class="hljs-strong">****</span><span class="hljs-strong">****</span><span class="hljs-strong">****</span><span class="hljs-strong">****</span><span class="hljs-strong">****</span><span class="hljs-strong">****</span><span class="hljs-strong">****</span><span class="hljs-strong">****</span><span class="hljs-strong">****</span>*</span></span></span>

<span class="hljs-comment">// coverage:ignore-file</span>
<span class="hljs-comment">// ignore_for_file: type=lint</span>
<span class="hljs-comment">// ignore_for_file: directives_ordering,unnecessary_import,implicit_dynamic_list_literal,deprecated_member_use</span>

<span class="hljs-keyword">import</span> <span class="hljs-string">'package:flutter/widgets.dart'</span>;

<span class="hljs-class"><span class="hljs-keyword">class</span> $<span class="hljs-title">AssetsIconsGen</span> </span>{
  <span class="hljs-keyword">const</span> $AssetsIconsGen();

  <span class="hljs-comment">/// <span class="markdown">File path: assets/icons/file<span class="hljs-emphasis">_add.png</span></span></span>
  AssetGenImage <span class="hljs-keyword">get</span> fileAdd =&gt; <span class="hljs-keyword">const</span> AssetGenImage(<span class="hljs-string">'assets/icons/file_add.png'</span>);

  <span class="hljs-comment">/// <span class="markdown"><span class="hljs-emphasis">List of all assets</span></span></span>
  <span class="hljs-built_in">List</span>&lt;AssetGenImage&gt; <span class="hljs-keyword">get</span> values =&gt; [fileAdd];
}

<span class="hljs-class"><span class="hljs-keyword">class</span> $<span class="hljs-title">AssetsImagesGen</span> </span>{
  <span class="hljs-keyword">const</span> $AssetsImagesGen();

  <span class="hljs-comment">/// <span class="markdown"><span class="hljs-emphasis">File path: assets/images/img.png</span></span></span>
  AssetGenImage <span class="hljs-keyword">get</span> img =&gt; <span class="hljs-keyword">const</span> AssetGenImage(<span class="hljs-string">'assets/images/img.png'</span>);

  <span class="hljs-comment">/// <span class="markdown"><span class="hljs-emphasis">List of all assets</span></span></span>
  <span class="hljs-built_in">List</span>&lt;AssetGenImage&gt; <span class="hljs-keyword">get</span> values =&gt; [img];
}

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Assets</span> </span>{
  Assets._();

  <span class="hljs-keyword">static</span> <span class="hljs-keyword">const</span> $AssetsIconsGen icons = $AssetsIconsGen();
  <span class="hljs-keyword">static</span> <span class="hljs-keyword">const</span> $AssetsImagesGen images = $AssetsImagesGen();
}

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AssetGenImage</span> </span>{
  <span class="hljs-keyword">const</span> AssetGenImage(<span class="hljs-keyword">this</span>._assetName);

  <span class="hljs-keyword">final</span> <span class="hljs-built_in">String</span> _assetName;

  Image image({
    Key? key,
    AssetBundle? bundle,
    ImageFrameBuilder? frameBuilder,
    ImageErrorWidgetBuilder? errorBuilder,
    <span class="hljs-built_in">String?</span> semanticLabel,
    <span class="hljs-built_in">bool</span> excludeFromSemantics = <span class="hljs-keyword">false</span>,
    <span class="hljs-built_in">double?</span> scale,
    <span class="hljs-built_in">double?</span> width,
    <span class="hljs-built_in">double?</span> height,
    Color? color,
    Animation&lt;<span class="hljs-built_in">double</span>&gt;? opacity,
    BlendMode? colorBlendMode,
    BoxFit? fit,
    AlignmentGeometry alignment = Alignment.center,
    ImageRepeat repeat = ImageRepeat.noRepeat,
    Rect? centerSlice,
    <span class="hljs-built_in">bool</span> matchTextDirection = <span class="hljs-keyword">false</span>,
    <span class="hljs-built_in">bool</span> gaplessPlayback = <span class="hljs-keyword">false</span>,
    <span class="hljs-built_in">bool</span> isAntiAlias = <span class="hljs-keyword">false</span>,
    <span class="hljs-built_in">String?</span> package,
    FilterQuality filterQuality = FilterQuality.low,
    <span class="hljs-built_in">int?</span> cacheWidth,
    <span class="hljs-built_in">int?</span> cacheHeight,
  }) {
    <span class="hljs-keyword">return</span> Image.asset(
      _assetName,
      key: key,
      bundle: bundle,
      frameBuilder: frameBuilder,
      errorBuilder: errorBuilder,
      semanticLabel: semanticLabel,
      excludeFromSemantics: excludeFromSemantics,
      scale: scale,
      width: width,
      height: height,
      color: color,
      opacity: opacity,
      colorBlendMode: colorBlendMode,
      fit: fit,
      alignment: alignment,
      repeat: repeat,
      centerSlice: centerSlice,
      matchTextDirection: matchTextDirection,
      gaplessPlayback: gaplessPlayback,
      isAntiAlias: isAntiAlias,
      package: package,
      filterQuality: filterQuality,
      cacheWidth: cacheWidth,
      cacheHeight: cacheHeight,
    );
  }

  ImageProvider provider({
    AssetBundle? bundle,
    <span class="hljs-built_in">String?</span> package,
  }) {
    <span class="hljs-keyword">return</span> AssetImage(
      _assetName,
      bundle: bundle,
      package: package,
    );
  }

  <span class="hljs-built_in">String</span> <span class="hljs-keyword">get</span> path =&gt; _assetName;

  <span class="hljs-built_in">String</span> <span class="hljs-keyword">get</span> keyName =&gt; _assetName;
}
</code></pre>
<p>In this code,</p>
<ol>
<li><p><code>$AssetsIconsGen</code> and <code>$AssetsImagesGen</code>: These classes represent your icon and image directories, respectively. Each asset within these directories gets a getter (for example, <code>fileAdd</code>, <code>img</code>) that returns an <code>AssetGenImage</code> object.</p>
</li>
<li><p><code>Assets</code> class: This is the main entry point for accessing all your generated assets. It provides static instances of <code>$AssetsIconsGen</code> and <code>$AssetsImagesGen</code> (for example, <code>Assets.icons</code>, <code>Assets.images</code>).</p>
</li>
<li><p><code>AssetGenImage</code> class: This utility class wraps the asset path and provides convenience methods like <code>image()</code> to directly create an <code>Image</code> widget and <code>provider()</code> to get an <code>ImageProvider</code>. The <code>path</code> getter provides the raw asset path if needed.</p>
</li>
</ol>
<h3 id="heading-5-using-generated-assets-in-your-code">5. Using Generated Assets in Your Code</h3>
<p>Now that your assets are type-safe and easily accessible, let's integrate them into your Flutter application.</p>
<p>First, create a <code>screens</code> folder inside your <code>lib</code> directory. Then, create a new file named <code>entry_screen.dart</code> inside the <code>lib/screens</code> folder and paste the following code:</p>
<p><code>lib/screens/entry_screen.dart</code>:</p>
<pre><code class="lang-dart"><span class="hljs-keyword">import</span> <span class="hljs-string">'package:flutter/material.dart'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'../gen/assets.gen.dart'</span>; <span class="hljs-comment">// Import the generated assets file</span>

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">EntryScreen</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">StatelessWidget</span> </span>{
  <span class="hljs-keyword">const</span> EntryScreen({Key? key}) : <span class="hljs-keyword">super</span>(key: key);

  <span class="hljs-meta">@override</span>
  Widget build(BuildContext context) {
    <span class="hljs-keyword">return</span> Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            <span class="hljs-comment">// Using a generated Image asset</span>
            Image.asset(Assets.images.img.path), <span class="hljs-comment">// Access the image using Assets.images.img.path</span>
            <span class="hljs-keyword">const</span> SizedBox(height: <span class="hljs-number">10</span>),
            <span class="hljs-keyword">const</span> Text(
              <span class="hljs-string">'Flutter Gen Assets'</span>,
              style: TextStyle(
                fontWeight: FontWeight.bold,
                fontSize: <span class="hljs-number">18</span>,
              ),
            ),
            <span class="hljs-keyword">const</span> SizedBox(height: <span class="hljs-number">10</span>),

            <span class="hljs-comment">// Using a generated Icon asset</span>
            Image.asset(Assets.icons.fileAdd.path), <span class="hljs-comment">// Access the icon using Assets.icons.fileAdd.path</span>
          ],
        ),
      ),
    );
  }
}
</code></pre>
<p>What’s going on in <code>entry_screen.dart</code>:</p>
<ol>
<li><p><code>import '../gen/assets.gen.dart';</code>: This line imports the generated <code>assets.gen.dart</code> file, making all your type-safe image and icon assets available.</p>
</li>
<li><p><code>Image.asset(Assets.images.img.path)</code>: Instead of a hardcoded string like <code>Image.asset('assets/images/img.png')</code>, we now use <code>Assets.images.img.path</code>. This is type-safe and benefits from IDE autocomplete, preventing errors and improving readability.</p>
</li>
<li><p><code>Image.asset(Assets.icons.fileAdd.path)</code>: Similarly, icons are accessed through <code>Assets.icons.fileAdd.path</code>.</p>
</li>
</ol>
<p>Next, modify your <code>main.dart</code> file to use the <code>EntryScreen</code> and the generated font.</p>
<p><code>lib/main.dart</code>:</p>
<pre><code class="lang-dart"><span class="hljs-keyword">import</span> <span class="hljs-string">'gen/fonts.gen.dart'</span>; <span class="hljs-comment">// Import the generated fonts file</span>
<span class="hljs-keyword">import</span> <span class="hljs-string">'screens/entry_screen.dart'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'package:flutter/material.dart'</span>;

<span class="hljs-keyword">void</span> main() {
  runApp(<span class="hljs-keyword">const</span> MyApp());
}

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyApp</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">StatelessWidget</span> </span>{
  <span class="hljs-keyword">const</span> MyApp({<span class="hljs-keyword">super</span>.key});

  <span class="hljs-meta">@override</span>
  Widget build(BuildContext context) {
    <span class="hljs-keyword">return</span>  MaterialApp(
      <span class="hljs-comment">// Using generated Font asset</span>
      theme: ThemeData(fontFamily: FontFamily.roboto), <span class="hljs-comment">// Apply the Roboto font family using FontFamily.roboto</span>
      debugShowCheckedModeBanner: <span class="hljs-keyword">false</span>,
      home: <span class="hljs-keyword">const</span> EntryScreen(),
    );
  }
}
</code></pre>
<p>In <code>main.dart</code>:</p>
<ol>
<li><p><code>import 'gen/fonts.gen.dart';</code>: This imports the generated <code>fonts.gen.dart</code> file, giving you access to the <code>FontFamily</code> class.</p>
</li>
<li><p><code>theme: ThemeData(fontFamily: FontFamily.roboto)</code>: Here, we're applying the <code>Roboto</code> font family to our entire <code>MaterialApp</code> theme using <code>FontFamily.roboto</code>. This is a type-safe way to reference your custom font.</p>
</li>
</ol>
<h3 id="heading-running-your-application">Running Your Application</h3>
<p>Save all your changes and run your Flutter application:</p>
<pre><code class="lang-bash">flutter run
</code></pre>
<p>You should see your application launch, displaying the image and icon, all managed efficiently and type-safely by <code>flutter_gen</code>.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1702275921470/244c906f-2a2a-4630-b3a4-89d2455a95fe.png" alt="Application launched" class="image--center mx-auto" width="1915" height="1001" loading="lazy"></p>
<h2 id="heading-important-considerations">Important Considerations</h2>
<p>There are a couple things to note here:</p>
<ol>
<li><p>Whenever you add, remove, or rename assets in your <code>assets/</code> folders, or modify the asset declarations in <code>pubspec.yaml</code>, you <em>must</em> rerun the code generation commands:</p>
<pre><code class="lang-bash"> flutter pub get
 flutter pub run build_runner build
</code></pre>
</li>
<li><p>For a more seamless experience, you can use the <code>watch</code> command with <code>build_runner</code>. This will automatically regenerate your asset files whenever changes are detected:</p>
<pre><code class="lang-bash"> flutter pub run build_runner watch
</code></pre>
<p> Keep this command running in a separate terminal window during development.</p>
</li>
</ol>
<h2 id="heading-conclusion">Conclusion</h2>
<p>By integrating <code>flutter_gen</code> into your Flutter workflow, you unlock a superior asset management experience characterized by type safety, reduced errors, improved maintainability, and enhanced collaboration.</p>
<p>This guide has provided you with a solid foundation to leverage this powerful package effectively, making your Flutter development journey smoother and more robust.</p>
<h3 id="heading-further-reading">Further Reading</h3>
<p>To explore more advanced configurations and features of <code>flutter_gen</code>, refer to the <a target="_blank" href="https://pub.dev/packages/flutter_gen">official <code>flutter_gen</code> package documentation page</a>.</p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
