Save files and images
Besides primitive values via saveWidgetData, you can write files into the same shared area your app and widgets use. The absolute path is stored under your chosen key so native widget code can load the file (for example with UIImage(contentsOfFile:) or BitmapFactory.decodeFile).
saveFile
Write arbitrary bytes and register the path:
import 'dart:convert';
import 'dart:typed_data';
final json = jsonEncode({'count': 3});
final path = await HomeWidget.saveFile(
'stats',
Uint8List.fromList(utf8.encode(json)),
extension: 'json',
);
- The file is written as
{container}/home_widget/{key}.{extension}. - On iOS the container is the app group (call
setAppGroupIdfirst). - On Android it is under the application support directory, in a
home_widgetsubdirectory.
key must be non-empty and must not contain /, \, .., or spaces. extension is normalized (e.g. json or .json); it must not contain path separators. These rules are enforced with assertions in debug builds.
saveImage
Decode an ImageProvider and save the first frame as PNG via saveFile:
await HomeWidget.saveImage(
'avatar',
NetworkImage('https://example.com/photo.png'),
);
Animated images use the first frame only.
Relationship to renderFlutterWidget
renderFlutterWidget renders a widget to a PNG and uses the same storage and saveWidgetData path behavior as saveFile with extension png.
Native widgets
Use the same approach as for rendered Flutter widgets: read the key with your platform’s widget APIs to get the path string, then load that path as a file or image. See the iOS and Android setup guides for accessing shared data from the widget extension or AppWidgetProvider.
Example
See the file_and_images example app on GitHub for how to use saveFile, saveImage, and load the stored files from Android and iOS home screen widgets.