getImageFromAsset static method
Loads an image from the asset and returns its bytes as a Uint8List.
This method is used to fetch image data (binary format) from the app's assets. It converts the image into a Uint8List which is the format required for processing or printing on devices like the Sunmi printer.
iconPath
is the path to the asset file in your project (e.g., 'assets/images/icon.png').
Returns a Future that resolves to the image data as Uint8List.
Throws an exception if the image cannot be loaded from the asset.
Implementation
static Future<Uint8List> getImageFromAsset(String iconPath) async {
try {
// Call the private method to read the bytes from the asset file.
return await _readFileBytes(iconPath);
} catch (e) {
// If there's an error, throw an exception with a message including the asset path.
// This helps identify what went wrong during the loading process.
throw Exception('Failed to load image from asset: $iconPath. Error: $e');
}
}