Flutter google_fonts Package FontFeature Error
Problem Statement
When using the google_fonts
package (version 6.2.0 or later) in a Flutter project, you may encounter a critical build error:
Error: 'FontFeature' isn't a type.
List<FontFeature>? fontFeatures,
^^^^^^^^^^^
Target kernel_snapshot failed: Exception
FAILURE: Build failed with an exception.
Execution failed for task ':app:compileFlutterBuildDebug'.
This error occurs because:
- Newer versions of
google_fonts
(≥6.2.0) use theFontFeature
class - Your Flutter SDK version may be too old to include this type
- The
FontFeature
class resides indart:ui
, which wasn't available in older SDK versions
Common symptoms include:
- Build failures during
flutter run
orflutter build
- Failed compilation even after downgrading the package
flutter clean
andflutter pub get
don't resolve the issue
WARNING
This issue is SDK-related - changing target devices won't fix it
Recommended Solutions
1. Upgrade Flutter SDK (Recommended)
The optimal solution is upgrading your Flutter SDK to ensure compatibility:
flutter upgrade
Then verify in your pubspec.yaml
:
environment:
sdk: '>=3.0.0 <4.0.0' # Ensure minimum SDK version is 3.0+
Why this works:
- Newer SDK versions include
dart:ui
withFontFeature
- Maintains compatibility with latest package versions
- Provides security patches and performance improvements
2. Downgrade google_fonts Package
If upgrading Flutter isn't feasible, downgrade the package:
- In
pubspec.yaml
:
dependencies:
google_fonts: 6.1.0 # Note: No caret (^) prefix
- Run:
flutter clean
flutter pub get
Compatibility matrix:
Package Version | Flutter Version Requirement |
---|---|
≥6.2.0 | Latest Flutter (recommended) |
6.1.0 | Works with older Flutter versions |
5.1.0 | Compatible with much older SDKs |
TIP
After downgrading, remove the caret (^
) before the version number to prevent accidental upgrades
Additional Troubleshooting Steps
If issues persist after downgrading:
- Clear build caches:
flutter clean
flutter pub cache repair
- Delete the
pubspec.lock
file and run:
flutter pub get
- Verify version resolution:
flutter pub deps | grep google_fonts
Conclusion
The FontFeature
error occurs due to SDK-package version mismatch. The best fix is:
Final Recommendations:
- Regularly update your Flutter SDK (
flutter upgrade
quarterly) - Monitor package changelogs when updating dependencies
- Validate SDK constraints in
pubspec.yaml
for 3rd-party packages
Most users report success with either upgrading Flutter or downgrading to google_fonts: 6.1.0
without the ^
prefix.