Skip to content

lookupKeyForAsset docs are misleading, and API is problematic on macOS #135302

@stuartmorgan-g

Description

@stuartmorgan-g

While working on macOS support for video_player I discovered that lookupKeyForAsset: doesn't seem to do what the docs suggest, and it doesn't seem to work the way it needs to on macOS; code that works in that plugin for loading videos from assets on iOS does not work on macOS.

The docs say:

/**
 * Returns the file name for the given asset. If the bundle with the identifier
 * "io.flutter.flutter.app" exists, it will try use that bundle; otherwise, it
 * will use the main bundle.  To specify a different bundle, use
 * `+lookupKeyForAsset:fromBundle`.
 *
 * @param asset The name of the asset. The name can be hierarchical.
 * @return the file name to be used for lookup in the main bundle.
 */

What it actually seems to return is not a file name, but a relative path from the main bundle. That appears to work for NSBundle lookup on iOS (I'm actually not sure why it does work), but not on macOS. The video_player example app's flow is:

iOS

  1. Dart code requests loading of the asset assets/Butterfly-209.mp4.
  2. Native code calls lookupKeyForAsset:@"assets/Butterfly-209.mp4" on the registrar and gets back Frameworks/App.framework/flutter_assets/assets/Butterfly-209.mp4 (the relative path from the main bundle).
  3. Native code calls [[NSBundle mainBundle] pathForResource:<the path above> ofType:nil] and gets back an absolute path.
  4. Native code loads that video.

macOS

  1. Dart code requests loading of the asset assets/Butterfly-209.mp4.
  2. Native code calls lookupKeyForAsset:@"assets/Butterfly-209.mp4" on the registrar and gets back Contents/Frameworks/App.framework/Resources/flutter_assets/assets/Butterfly-209.mp4 (still the correct relative path from the main bundle).
  3. Native code calls [[NSBundle mainBundle] pathForResource:<the path above> ofType:nil] and gets back nil.
  4. Native code fails to load the video because the path is nil.

So while our API does itself have the same behavior on macOS and iOS, that behavior doesn't actually work for what it's supposed to do on macOS.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work lista: desktopRunning on desktopa: pluginsSupport for writing, building, and running plugin packagesengineflutter/engine related. See also e: labels.platform-iosiOS applications specificallyplatform-macBuilding on or for macOS specificallyteam-macosOwned by the macOS platform teamtriaged-macosTriaged by the macOS platform team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions