Skip to content

Unable to load image asset on low-res devices if only high-resolution variants exist #127090

@yeshu-cn

Description

@yeshu-cn

Is there an existing issue for this?

Description:

After upgrading to Flutter 3.10.1, I've noticed that low resolution devices are unable to correctly utilize high resolution image assets. Within my image resources directory, I have a 3.0x folder that contains the icon_apps.png image.

In Flutter 3.7.12, when I utilized image_resolution on a 2x device, candidateVariants would correctly return assets/images/3.0x/icon_apps.png. However, in Flutter 3.10.1, I observed that a 2x device would return two candidateVariants: assets/images/icon_apps.png and assets/images/3.0x/icon_apps.png.

Below is the related source code from Flutter 3.10.1 SDK for image selection:

AssetManifest.loadFromAssetBundle(chosenBundle)
      .then((AssetManifest manifest) {
        final Iterable<AssetMetadata>? candidateVariants = manifest.getAssetVariants(keyName);
        final AssetMetadata chosenVariant = _chooseVariant(
          keyName,
          configuration,
          candidateVariants,
        );
      });

Steps to reproduce

  • Create a project in Flutter 3.10.1, and within the assets/images/ directory, create a 3.0x/ directory, and place the icon_apps.png image in this directory.
  • Declare the image assets in the pubspec.yaml file.
flutter:
  assets:
    - assets/images/icon_apps.png
  • On a 2x device, load the icon_apps.png image :Image.asset("asset/images/icon_apps.png"),.

Expected results

On low resolution devices, even if the device resolution is lower than the image asset resolution, it should still be able to correctly utilize high resolution image assets.

Actual results

On low resolution devices, it's not possible to utilize high resolution image assets because image_resolution returns incorrect candidateVariants and chooses the wrong variant.

Code sample

asset dir files:

assets
    - images
        - 3.0x
            - icon_apps.png 

pubsepc.yaml

flutter:
  assets:
    - assets/images/icon_apps.png
Image.asset("asset/images/icon_apps.png"),

Screenshots or Video

No response

Logs

Exception: Asset not found
#0 PlatformAssetBundle.loadBuffer (package:flutter/src/services/asset_bundle.dart:369:7)

#1 AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:811:18)

Flutter Doctor output

$ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.10.1, on macOS 13.0.1 22A400 darwin-arm64, locale zh-Hans-CN)
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.2)
[✓] Xcode - develop for iOS and macOS (Xcode 14.3)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2022.2)
[✓] IntelliJ IDEA Community Edition (version 2022.2.3)
[✓] VS Code (version 1.78.1)
[✓] Connected device (3 available)
[✓] Network resources

• No issues found!
(base)

Metadata

Metadata

Assignees

Labels

P1High-priority issues at the top of the work lista: assetsPackaging, accessing, or using assetsc: regressionIt was better in the past than it is nowfound in release: 3.10Found to occur in 3.10found in release: 3.11Found to occur in 3.11has reproducible stepsThe issue has been confirmed reproducible and is ready to work ontoolAffects the "flutter" command-line tool. See also t: labels.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions