import 'package:flutter/material.dart';
enum MyIcons implements IconData {
test(58332);
const MyIcons(this.codePoint);
@override
final int codePoint;
@override
String get fontFamily => 'MaterialIcons';
@override
final String? fontPackage = null;
@override
final bool matchTextDirection = false;
}
void main() {
runApp(
MaterialApp(
home: Scaffold(
appBar: AppBar(
leading: const Icon(MyIcons.test),
),
),
),
);
}
Given the code above in a fresh flutter create project, run the flutter build apk. You'll see fail:
➜ icon_shaking flutter build apk
💪 Building with sound null safety 💪
Target aot_android_asset_bundle failed: IconTreeShakerException: Invalid ConstFinder result. Expected "fontPackage" to be a String, "fontFamily" to be a String, and "codePoint" to be an int, got: {codePoint: 58332, fontPackage: null, matchTextDirection: false, index: 0, _name: test}.
To disable icon tree shaking, pass --no-tree-shake-icons to the requested flutter build command
FAILURE: Build failed with an exception.
* Where:
Script '/Users/albert/fvm/versions/3.7.12/packages/flutter_tools/gradle/flutter.gradle' line: 1157
* What went wrong:
Execution failed for task ':app:compileFlutterBuildRelease'.
> Process 'command '/Users/albert/fvm/versions/3.7.12/bin/flutter'' finished with non-zero exit value 1
* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 12s
Running Gradle task 'assembleRelease'... 13.1s
Gradle task assembleRelease failed with exit code 1
This is because of the fontFamily getter. If I'd replace the getter with a normal field (final fontFamily = 'MaterialIcons';) then it works correctly.
The same problem happens with codePoint and fontPackage.
Of course, the MaterialIcons font is used here just as an example. In our project we use a custom icon font and provide the IconData with such enum.
Here's the source of the thrown exception:
|
throw IconTreeShakerException._( |
|
'Invalid ConstFinder result. Expected "fontPackage" to be a String, ' |
|
'"fontFamily" to be a String, and "codePoint" to be an int, ' |
|
'got: $iconDataMap.'); |
flutter --version
Flutter 3.7.12 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 4d9e56e694 (3 weeks ago) • 2023-04-17 21:47:46 -0400
Engine • revision 1a65d409c7
Tools • Dart 2.19.6 • DevTools 2.20.1
Given the code above in a fresh
flutter createproject, run theflutter build apk. You'll see fail:This is because of the
fontFamilygetter. If I'd replace the getter with a normal field (final fontFamily = 'MaterialIcons';) then it works correctly.The same problem happens with
codePointandfontPackage.Of course, the
MaterialIconsfont is used here just as an example. In our project we use a custom icon font and provide theIconDatawith such enum.Here's the source of the thrown exception:
flutter/packages/flutter_tools/lib/src/build_system/targets/icon_tree_shaker.dart
Lines 322 to 325 in 9b230d2