Skip to content

Consider tightening nullable compress return types once native failures throw CompressError #396

Description

@AlexV525

Motivation

Follow-up to #252. Once native compress failures propagate to Dart as CompressError instead of a silent null return (Step 1 — see the tracking work following that issue), the currently-nullable APIs have no legitimate "returns null on success" cases left. Every remaining null path is a failure signal that would be more accurately expressed as an exception.

Proposal

Tighten these signatures from Future<T?> to Future<T>:

  • FlutterImageCompress.compressWithFile — Future<Uint8List?> → Future<Uint8List>
  • FlutterImageCompress.compressAndGetFile — Future<XFile?> → Future<XFile>
  • FlutterImageCompress.compressAssetImage — Future<Uint8List?> → Future<Uint8List> (empty asset bundle bytes → throw CompressError(...))

compressWithList is already non-nullable and stays that way.

Null-source inventory

For reference — every existing null-return path is either dead code, a failure signal, or a latent bug:

API Null source (pre-Step-1) After Step 1
compressWithFile if (!support) return null; — dead code (checkSupportPlatform throws); native reply(null)/result(nil) on failure Both paths gone: dead code removed, native failure translates to CompressError
compressAndGetFile Same as above Same
compressAssetImage Same, plus uint8List.isEmpty → return null on empty asset bundle Empty-bundle case becomes throw CompressError('Empty asset bundle bytes for \$assetName')
compressWithList (common) Was already non-nullable but silently propagated null → runtime TypeError Fixed in Step 1

Why this is breaking

Any caller doing if (result == null) or result?.path breaks compilation. Migration is straightforward — replace null-checks with try/catch (CompressError) — but needs a major-version bump and a migrate.md section.

Not blocked on anything but coordination

Technically ready to land once Step 1 is in. Waiting on:

  1. A convenient major-version window.
  2. Deciding whether to bundle other breaking changes (e.g. web-platform compressAndGetFile shape from [Feature request] Return XFile or Path instead of Uint8List for compressions #281 discussion) into the same release.

Filing this as a tracking issue so we don't lose the reasoning.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions