Skip to content

Best Practice

Yu Ohama / Midori edited this page Feb 24, 2025 · 38 revisions

Use try-catch

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

try {
  await Gal.putImage($path);
} 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

putImage requires a temporary file, but is safe because the extension of that file and the output file will always be the same.

putImageBytes does not require a temporary file, but instead the extension is automatically determined by the OS. In many cases this will not be a problem, but minorities should be aware of this if they wish to specify an extension or OS.

Feature putImage putImageBytes
File Type Same as input Auto detect
Temporary file Necessary Unnecessary