Skip to content

Decode synchronously with ui.decodeImageFromPixelsSync when available - #79

Open
Coinners wants to merge 1 commit into
fluttercommunity:masterfrom
Coinners:decode-image-from-pixels-sync
Open

Coinners wants to merge 1 commit into
fluttercommunity:masterfrom
Coinners:decode-image-from-pixels-sync

Conversation

@Coinners

Copy link
Copy Markdown

What this changes

Flutter 3.41 added ui.decodeImageFromPixelsSync. It returns a ui.Image right away instead of through a callback. This PR uses it so the blurhash can show up in the first frame. Today, BlurHash shows color for at least one frame while decodeImageFromPixels finishes, and that flash is visible in lists and on page transitions.

The sync API only works with Impeller:

  • on the web it throws UnimplementedError
  • with Skia it throws a String from the engine

The package catches either error and falls back to the existing async path (decodeImageFromPixels, or the BMP codec on the web). After the first failure it stops trying the sync API, so it doesn't throw on every decode.

Changes

  • New blurHashDecodeImageSync: returns a ui.Image?, or null when sync decoding isn't supported. It still throws on an invalid hash, like the other decode functions.
  • BlurHash widget: tries the sync decode first and renders it directly. If that fails, it uses the existing FutureBuilder path. onDecoded still fires asynchronously, as before.
  • BlurHashImage: returns a SynchronousFuture when sync decoding works, so the image is available immediately.
  • blurHashDecodeImage: uses the sync API internally when possible. Its signature and behavior are unchanged.
  • Refactor: blurHashDecode and optimizedBlurHashDecode now wrap private sync functions. Both were already synchronous inside Future.value, and their public API is unchanged.

⚠️ Minimum Flutter version

Dart can't check whether a top-level function exists at compile time, so this PR raises the constraint to flutter: ">=3.41.0" / sdk: ">=3.11.0 <4.0.0". This probably deserves a minor version bump (e.g. 0.10.0). I left version: unchanged and put the changes under an "Unreleased" heading in the CHANGELOG.

Testing

  • flutter analyze: no issues
  • flutter test (Skia): all tests pass, blurHashDecodeImageSync returns null, and decoding falls back to async
  • flutter test --enable-impeller: all tests pass, the sync decode returns an image, and BlurHash renders an Image in the first frame

Unrelated issue

BlurHashState._decodeImage calls _image.whenComplete(...), and that returns a new future nobody listens to. With an invalid hash, this produces an unhandled async error. The behavior already exists and this PR doesn't change it. I can fix it in a separate PR.

🤖 Generated with Claude Code

Use ui.decodeImageFromPixelsSync (Flutter 3.41+) so BlurHash and
BlurHashImage can show the blurhash in the very first frame instead of
flashing the placeholder color. The API throws on the web and when
Impeller is disabled, so failures are caught, remembered, and decoding
falls back to the existing asynchronous path.

- Add blurHashDecodeImageSync, which returns null when unsupported
- blurHashDecodeImage uses the sync API internally when possible
- Raise the minimum Flutter version to 3.41

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant