What is code-splitting in Flutter, and how does it help?
#performance#optimization#code-splitting
Answer
Overview
Code splitting is a technique to break your app's code into smaller chunks that are loaded only when needed, rather than loading everything upfront. This reduces initial load time and app bundle size.
Code Splitting in Flutter Web
Code splitting is primarily relevant for Flutter Web. On mobile (iOS/Android), the AOT-compiled binary is already optimized. On web, JavaScript bundles can become large.
bash# Build Flutter web with deferred loading enabled flutter build web
Deferred Loading (Dart's Built-in Code Splitting)
Dart supports code splitting via the keyword. Libraries are loaded lazily on first use.textdeferred as
dart// Import the library as deferred import 'package:my_app/features/analytics/analytics.dart' deferred as analytics; // Load and use it only when needed Future<void> initAnalytics() async { await analytics.loadLibrary(); // Downloads the library chunk on demand analytics.Analytics.init(); } // In a button handler ElevatedButton( onPressed: () async { await analytics.loadLibrary(); analytics.Analytics.trackEvent('purchase'); }, child: Text('Complete Purchase'), )
Practical Example — Heavy Feature Screen
dartimport 'package:my_app/screens/video_editor.dart' deferred as videoEditor; class HomeScreen extends StatelessWidget { Widget build(BuildContext context) { return Scaffold( body: Center( child: ElevatedButton( onPressed: () async { // Only downloads VideoEditor code when user taps await videoEditor.loadLibrary(); Navigator.push( context, MaterialPageRoute( builder: (_) => videoEditor.VideoEditorScreen(), ), ); }, child: Text('Open Video Editor'), ), ), ); } }
How It Helps
| Without Code Splitting | With Code Splitting |
|---|---|
| Entire app code loaded upfront | Only critical code loaded at startup |
| Larger initial JS bundle (web) | Smaller initial bundle |
| Slower first meaningful paint | Faster initial load |
| Unused features still downloaded | Features downloaded on demand |
On Mobile vs Web
| Platform | Code Splitting Effect |
|---|---|
| Flutter Web | ✅ Significant — reduces JS bundle, faster page load |
| Android/iOS | ⚠️ Limited — AOT binary is already optimized; Play/App Store handles delivery |
Android App Bundle (Mobile Code Splitting)
bash# Build Android App Bundle — Google Play delivers only what the device needs flutter build appbundle # Play Store handles: architecture splits, language splits, density splits # Users download only APK subset for their device
When to Use Deferred Loading
| Scenario | Use Deferred? |
|---|---|
| Feature used by <30% of users | ✅ Yes |
| Heavy library (charts, PDF, video) | ✅ Yes |
| Admin/settings screens | ✅ Yes |
| Core navigation and auth | ❌ No — load immediately |
| Frequently used screens | ❌ No — deferred causes delay on first open |
Key Benefit: Deferred loading reduces the Time To Interactive (TTI) for Flutter Web apps by deferring heavy code until it's actually needed. On mobile, use App Bundles to let the Play Store handle code/asset splitting per device.