Skip to content

Best Practice

Midori edited this page Aug 19, 2023 · 34 revisions

Use try-catch

Use try-catch in production code. Gal throws a GalException if an error occurs while saving media. With GalException.type.message you can get a message for each type.

try {
  final path = await getFilePath('assets/done.jpg');
  await Gal.putImage(path, album: album);
} on GalException catch (e) {
  log(e.type.message);
}

enum GalExceptionType {
  accessDenied,
  notEnoughSpace,
  notSupportedFormat,
  unexpected;

  String get message => switch (this) {
        accessDenied => 'You do not have permission to access the gallery app.',
        notEnoughSpace => 'Not enough space for storage.',
        notSupportedFormat => 'Unsupported file formats.',
        unexpected => 'An unexpected error has occurred.',
      };
}

putImage vs putImageBytes

If there is no particular reason, you should use putImage.

The primary advantage of putImage is file extension is to be taken over from the filename. Conversely, with putImageBytes, this is determined automatically and may result in unintended behavior with minor devices and extensions.

Clone this wiki locally