Skip to content

Support wildcard/globs in flutter: assets: YAML #27801

Open

Description

@matanlurey

See #4890 (comment).

I'd like to write:

flutter:
  assets:
    - assets/icons/**.png

... instead of ...

flutter:
  assets:
    - assets/icons/country_flags/
    - assets/icons/currency_codes/
    - assets/icons/credit_card_vendors/

Activity

  1. added
    toolAffects the "flutter" command-line tool. See also t: labels.
    on Feb 11, 2019
  2. jonahwilliams commented on Feb 12, 2019

    @jonahwilliams
    Contributor

    Looked into this today.

    The asset parsing code is in https://github.com/flutter/flutter/blob/master/packages/flutter_tools/lib/src/asset.dart#L524. The difficulty is that we cannot just start using Globs as

    • the flutter tool uses interfaces which implement dart:io, but aren't dart:io. Glob's listSync returns a different file type than we expect and its not compatible with our test infra.
    • The resolution logic is fairly specialized, and should probably just be refactored as a part of this change. It might make more sense to build out our own glob logic.
  3. marnberg commented on Feb 13, 2019

    @marnberg

    Thank you @jonahwilliams for looking into this. This is been on our watch list for a long time since we have images in assets/1.0x assets/2.0x and so on. Flutter picks the right asset listing the path without the scale factor but currently we have to list every asset in the yaml file. This is a big issue because we build multiple versions of our app with different assets.

  4. ph55 commented on Jan 18, 2020

    @ph55

    Another use case.
    Loading query language files. For example GraphQL files. They will usually will stick to it's library and won't be placed in one directory.

    /lib/user/get_users.gql
    /lib/user/register.gql
    /lib/todo/get_todos.gql
    

    So pattern in this case would be any .gql file in lib folder:

    flutter:
      assets:
        - lib/**/*.gql
  5. nimi0112 commented on May 24, 2021

    @nimi0112

    @matanlurey @jonahwilliams what is wrong with writing ?

    assets:
        - assets/images/
  6. triallax commented on Aug 13, 2021

    @triallax

    @nimi0112 I don't know about the others, but I use macOS, and I caught .DS_Store files being put in assets.

  7. added
    P3Issues that are less important to the Flutter project
    on May 31, 2022
  8. added a commit that references this issue on Oct 30, 2022
  9. JaffaKetchup commented on Oct 5, 2023

    @JaffaKetchup
    Contributor

    Bumping this 2 years later :)

    As a workaround, what if there was a flutter pub registerassets tool that read from a file supporting wildcards and auto generated the necessary pubspec?

  10. Conrad33 commented on Jul 4, 2024

    @Conrad33

    Another use case is with the new Asset transformation feature. For example something like:

    flutter:
      assets:
        - path: assets/images/
          transformers:
            - package: vector_graphics_compiler

    doesn't work if the images folder contains anything other than .svg files.
    Instead we could do:

    flutter:
      assets:
        - assets/images/
        - path: assets/images/**.svg
          transformers:
            - package: vector_graphics_compiler

    and only transform the assets we want to, ignoring any .png and .jpg files in the same folder.

  11. added
    tool-still-validIssues that have been deemed still valid as part of the Flutter Tools issue cleanup initiative.
    on Jun 30, 2025
  12. spydon commented on Oct 8, 2025

    @spydon
    Contributor

    The Dart workspaces definition now supports globs (dart-lang/pub#4680), it would be nice to have it consistent in the pubspec file so that assets also supports globs. (And if there are other places where globs also would make sense)

  13. borzos6 commented on Sep 2, 2026

    @borzos6

    Adding a data point: our asset tree is nested by category (assets/characters/*, assets/backgrounds/*, etc.), and every new subfolder/file needs a manual pubspec.yaml entry. Two options would both solve it for us:

    Current workaround is keeping folders flat to dodge the maintenance, which fights against organizing assets sensibly. Either fix removes real day-to-day upkeep for projects with any depth to their asset structure.

  14. JaffaKetchup commented on Sep 2, 2026

    @JaffaKetchup
    Contributor

    Hey, I'm happy to work on this (the glob approach). Trying to understand the background.

    I've gone through the previous discussions, back to when the ability to add a whole directory at a time was added (#4890 & #16413 I think), and found this article written by the contributor then:

    #4890 (comment) -> https://medium.com/flutter-community/contributing-to-flutter-part-3-4b96d924d8ba

    And also #27801 (comment) from @jonahwilliams above.

    So it looks like the bigger issue is around variants, which makes sense. And the other issue is that package:glob isn't directly compatible with the objects used in the tool.

    The primary issue that the article author seemed to be concerned about was changing behaviour in future Flutter versions. I would think that this is less of a risk now everything's much more mature (please correct me if I'm wrong).

    To just share the problem here:

    assets/hero.png
    assets/3.0x/hero.png
    assets/landscape/hero.png
    assets/landscape/3.0x/hero.png

    Means two assets, 'hero.png' and 'landscape/hero.png', each with two variants. The author mentions he was concerned about future changes in behaviour meaning this becomes recognised differently.

    But if this is instead declared just as:

    assets/**

    Then we need to figure this out, rather than just having four different assets.

    Now I think the solution that the author alludes to is using regex to say that any directory in the format:

    [0-9]+\.[0-9]+x

    is a variant directory.

    It is unclear to me whether the variant directory is always the last directory in the path (if present). I think this is the case though, which does make it slightly easier. I also don't know if an asset under a variant path can be referred to directly by its path if a specific variant is wanted - I guess it must be, for example in the case that the non-variant version is not declared.

    The author says:

    As easy as it sounds it can bring a couple unexpected surprises if both the scanning (build time) and painting (run time) code don’t agree. Adding to it that currently in Flutter variants are used exclusively for image resolution, but if later it’s expanded in a similar way as in Android to cover things such as localization, screen size or orientation, and we could end up in a situation where upgrading the flutter version could break your build.

    I don't quite understand what this is getting at. Surely it's just the build process' job to gather all the files and nothing else needs to change? And I've not seen any discussion about the second part - but yes, that would make the naive approach of using a regex to get resolution variant directories break. Perhaps a solution is to manually indicate for each declared path whether the last directory in the tree should be considered a variant - although that feels like a poor solution (and I can't see how to make that non-breaking).

    It sounds like the original author did intend to perhaps come back and add it in at some point, but it looks like it never happened.

    Can anyone confirm whether I've missed anything? Also the assets.dart file is quite large by the looks of it, so wondering if there were any existing plans to try and simplify or take it apart it if at all possible.

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

    P3Issues that are less important to the Flutter projecta: assetsPackaging, accessing, or using assetsteam-toolOwned by Flutter Tool teamtoolAffects the "flutter" command-line tool. See also t: labels.tool-still-validIssues that have been deemed still valid as part of the Flutter Tools issue cleanup initiative.triaged-toolTriaged by Flutter Tool team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions