Skip to content

Marking IconData's constructor parameters as @mustBeConst #181344

Description

@dcharkes

The class IconData has a special constraints in its use due to the Flutter Icon Tree Shaker.

The constructor:

const IconData(
this.codePoint, {
this.fontFamily,
this.fontPackage,
this.matchTextDirection = false,
this.fontFamilyFallback,
});

The tree-shaker expects const instances with 3 specific fields with specific types:

Map<String, List<int>> _parseConstFinderResult(_ConstFinderResult constants) {
final result = <String, List<int>>{};
for (final Map<String, Object?> iconDataMap in constants.constantInstances) {
final Object? package = iconDataMap['fontPackage'];
final Object? fontFamily = iconDataMap['fontFamily'];
final Object? codePoint = iconDataMap['codePoint'];
if ((package ?? '') is! String || (fontFamily ?? '') is! String || codePoint is! num) {
throw IconTreeShakerException._(
'Invalid ConstFinder result. Expected "fontPackage" to be a String, '
'"fontFamily" to be a String, and "codePoint" to be an int, '
'got: $iconDataMap.',
);
}

In package:meta we have mustBeConst which will make the analyzer give warnings on non-const arguments to parameters marked with this annotation.

I propose we add the annotation to fontPackage, fontFamily, and codePoint. This will make a warning show up before running the icon-tree-shaker. Giving developers earlier feedback.

(Context: I'm working on a generalized mechanism of the icon tree shaker and underlying const finder that should give this kind of powers to a package. In the generalized mechanism it would be fine to either have const IconData(...) or IconData(const ...). The latter would be an extension of the capabilities that are today allowed by the Flutter icon tree shaker. For the generalized mechanism, we'd be suggesting people annotate their definitions with @mustBeConst for early feedback in the IDE to the users of their API.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    P1High-priority issues at the top of the work listframeworkflutter/packages/flutter repository. See also f: labels.team-frameworkOwned by Framework teamtriaged-frameworkTriaged by Framework team

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions