Files
immich/mobile/lib/utils/image_converter.dart
T
Adam GastineauandGitHub e7aace436d chore(mobile): Apply stricter linting rules for correctness (#30372)
* chore(mobile): Apply stricter linting rules for correctness

* Added discarded_futures rule
2026-07-30 19:39:24 +00:00

31 lines
1.1 KiB
Dart

import 'dart:async';
import 'dart:typed_data';
import 'dart:ui';
import 'package:flutter/material.dart';
/// Converts a Flutter [Image] widget to a [Uint8List] in PNG format.
///
/// This function resolves the image stream and converts it to byte data.
/// Returns a [Future] that completes with the image bytes or completes with an error
/// if the conversion fails.
Future<Uint8List> imageToUint8List(Image image) async {
final Completer<Uint8List> completer = Completer();
image.image
.resolve(ImageConfiguration.empty)
.addListener(
ImageStreamListener((ImageInfo info, bool _) {
unawaited(
info.image.toByteData(format: ImageByteFormat.png).then((byteData) {
if (byteData != null) {
completer.complete(byteData.buffer.asUint8List());
} else {
completer.completeError('Failed to convert image to bytes');
}
}),
);
}, onError: (exception, stackTrace) => completer.completeError(exception)),
);
return completer.future;
}