Optimizing Asset Management in Flutter: Dynamic Inclusion for Platform-Specific and Common Assets

Asfaque Ahmed
3 min readApr 10, 2024

--

Flutter, Google’s versatile UI toolkit, is all about making app development smoother and more efficient. And when it comes to crafting apps that feel right at home on both Android and iOS platforms, managing assets effectively is key.

In this article, we’ll take a laid-back stroll through a simple yet super effective method for organizing assets specifically for Android and iOS within your Flutter project. So kick back, relax, and let’s dive in!

Understanding the Challenge:

Consider a scenario where you’re developing a Flutter app that includes platform-specific assets for Android and iOS. You have different sets of assets tailored to each platform’s design guidelines. Additionally, there are some common icons shared between both platforms.

The Solution: Dynamic Asset Loading

Dynamic asset loading enables Flutter apps to adapt to the user’s device OS at runtime and load assets accordingly. This approach eliminates the need for separate asset configurations for each platform, simplifying development and maintenance.

Implementation

  • Organize Assets: Start by organizing assets into directories based on their platform or commonality. For example:
assets/
├── android/
│ ├── android_specific_asset.png
│ └── shared_asset.png
├── ios/
│ ├── ios_specific_asset.png
│ └── shared_asset.png
└── common/
└── common_asset.png
  • Update pubspec.yaml: In your pubspec.yaml file, specify the asset directories for both Android and iOS, along with any common assets:
flutter:
assets:
- assets/common/
- assets/android/
- assets/ios/
  • Dynamically Load Assets: Now make a dart file to utilize dart’s Platform class to detect the current platform at runtime and load assets accordingly:
import 'dart:io';

String _getPlatformAssetPath() {
if (Platform.isAndroid) {
return 'assets/android/';
} else if (Platform.isIOS) {
return 'assets/ios/';
} else {
throw UnsupportedError('Platform not supported');
}
}

String _getCommonAssetPath()=>"assets/common/";

String getPlatformAsset(String assetName) {
String assetPath = _getPlatformAssetPath();
return '$assetPath$assetName';
}

String getCommonAsset(String assetName) {
String assetPath = _getCommonAssetPath();
return '$assetPath$assetName';
}

Special scenario

Picture this: you’ve got some massive assets chilling in your Android and iOS folders, right? We’re talking about those hefty images, videos, or whatever else you’ve got that’s eating up space like it’s nobody’s business. Now, if we just toss all of these into our app bundle or IPA, well, let’s just say things are gonna get real chunky real fast. Nobody wants to be downloading a behemoth of an app, right?

But fear not! We’ve got a slick trick up our sleeves to keep things nice and trim: conditional assets in the pubspec.yaml file. This little gem allows us to include assets only where they're absolutely necessary, depending on which platform we're rocking

flutter:
assets:
- assets/common/

# Include Android-specific assets
android:
assets:
- assets/android/

# Include iOS-specific assets
ios:
assets:
- assets/ios/

Benefits

  • Optimized Image Quality: By including platform-specific images optimized for Android and iOS devices, you ensure that users receive high-quality images tailored to their device’s specifications.
  • Reduced App Size: Conditional inclusion of assets allows you to include only the necessary images for each platform, minimizing the overall size of the app bundle and improving download times.
  • Consistent User Experience: By adhering to platform-specific design guidelines and providing optimized images, you enhance the overall user experience and maintain consistency across different devices and platforms.

--

--

Asfaque Ahmed
0 Followers

Meet Asfaque, a Flutter virtuoso 🚀 Crafting captivating apps with passion and precision for 6 years. Let's build something beautiful!