Search…
Assets/Resources
Flutter allows you to bundle assets (graphics, sounds, config files...) along with your flutter application however dart cli applications do not have this ability.
DCli provides a set of asset management tools and an api to work around this limitation.
DCli refers to assets as 'resources' to differentiate them from flutter assets.
If you use resources in a package that will be publish to pub.dev, remember there is a 10MB limit on the entire dart package.
DCli does this by packaging an resource into a .dart library and then providing an api that allows you to unpack the resources at runtime.
DCli expects all resources to located within your dart project under:
1
<project root>/resources
Copied!

Packing resources

To pack your resources you run:
1
dcli pack
Copied!
The 'pack' command will scan the '<project root>/resources' directory and all subdirectories. Each file that it finds will be converted to a dart library under:
1
<project root>/lib/src/dcli/resources/generated
Copied!
Each library name is generated using a uuid of the form <uuid>.g.dart
So the following resources:
1
<project root>/resources
2
/images/photo.png
3
/data/zips/installer.zip
Copied!
Will result in to:
1
<project root>/lib/src/dcli/resources/generated
2
/resource_registry.g.dart
3
/aadafaasdf.g.dart
4
/aalwhkciyge.g.dart
Copied!
The contents of each resource is base64 encoded into a multi-line string. So the photo.png.dart file will something look like:
1
class Bcaaebbbbefe extends PackedResource {
2
3
// PackedResource
4
const Bcaaebbbbefe() : super(
5
'''
6
/9j/4aE0RXhpZgAASUkqAAgAAAANAAABAwABAAAAwA8AAAEBAwABAAAA0AsAAA8BAgAHAAAAqgAAABAB
7
AgAIAAAAsQAAABIBAwABAAAAAQAAABoBBQABAAAAuQAAABsBBQABAAAAwQAAACgBAwABAAAAAgAAADEB
8
AgAVAAAAyQAAADIBAgAUAAAA3gAAABMCAwABAAAAAQAAAGmHBAABAAAA8gAAACWIBAABAAAAgwMAAGQE
9
AABHb29nbGUAUGl4ZWwgNQBIAAAAAQAAAEgAAAABAAAASERSKyAxLjAuMzg4Nzg0NzYyemQAMjAyMTox
10
''');
11
}
Copied!

Resource Registry

As part of the packing process DCli also creates a registry of the packed resources. This is done by creating a dart library called:
<project root>/lib/src/dcli/resources/generated/resource_registry.g.dart
The contents of the 'resource_registry.dart' are of the form.
1
import 'package:dcli/dcli.dart';
2
import 'Bdcdfbdfdfa.g.dart';
3
import 'Bffeaaac.g.dart';
4
5
/// GENERATED -- GENERATED
6
///
7
/// DO NOT MODIFIY
8
///
9
/// This script is generated via [Resource.pack()].
10
///
11
/// GENERATED - GENERATED
12
13
class ResourceRegistry {
14
15
/// Map of the packed files.
16
/// Use the path of a packed file (relative to the resource directory)
17
/// to access the packed resource and then call [PackedResource].unpack()
18
/// to unpack the file.
19
/// ```dart
20
/// ResourceRegistry.resources['rules.yaml'].unpack(join(HOME, '.mysettings', 'rules.yaml'));
21
/// ```
22
static const Map<String, PackedResource> resources = {
23
'test.me' : Bdcdfbdfdfa(),
24
'PXL_20211104_224740653.jpg' : Bffeaaac(),
25
};
26
}
27
Copied!

Unpacking resources

DCli provides an api that allows your script to unpack its resources at run time.
1
ResourceRegistry().resources['<relative filename>'].unpack(String localPath)
Copied!
The resources field is a map and the key to the map is the original path to the packed file 'relative' to the resources directory.
e.g.
1
const filename = 'PXL_20211104_224740653.jpg';
2
3
final jpegResource = ResourceRegistry.resources[filename];
4
5
final pathToConfigs = join(HOME, '.myapp');
6
if (!exists(pathToConfigs)
7
{
8
createDir(pathToConfigs);
9
}
10
jpegResource!.unpack(join(pathToConfigs, filename);
11
Copied!

Automating packing of resources

If you use pub_release to publish to pub.dev then you can create a pre-release hook to have pub_release package your resources.
Last modified 16d ago