Question #67EasyFlutter Basics

Flutter what is tree shaken ?

#flutter

Answer

What is Tree Shaking in Flutter?

Tree shaking is an optimization technique used by Flutter's compiler to eliminate unused code from the final application bundle. It works by analyzing the entire codebase during compilation and removing any code, classes, methods, or libraries that are not actually referenced or used in the application.

How Tree Shaking Works

Tree shaking operates at compile time and follows these steps:

  1. Dependency Analysis: The compiler builds a dependency graph starting from the main entry point (
    text
    main()
    function)
  2. Reachability Check: It marks all code that is reachable through function calls, imports, and references
  3. Dead Code Elimination: Any code that is not marked as reachable is removed from the final bundle
  4. Bundle Optimization: The resulting package contains only the code that is actually used

Benefits of Tree Shaking

BenefitDescription
Reduced Bundle SizeRemoves unused code, resulting in smaller APK/IPA files
Faster DownloadSmaller app size means faster downloads for users
Improved PerformanceLess code to parse and execute at runtime
Better Memory UsageOnly necessary code is loaded into memory

Example of Tree Shaking

dart
// library_utils.dart
class UsedClass {
  void usedMethod() {
    print('This is used');
  }
}

class UnusedClass {
  void unusedMethod() {
    print('This will be removed by tree shaking');
  }
}

// main.dart
import 'library_utils.dart';

void main() {
  final obj = UsedClass();
  obj.usedMethod(); // Only UsedClass is used
  // UnusedClass is never instantiated, so it will be removed
}

In this example,

text
UnusedClass
and its methods will be completely removed from the final build.

When Tree Shaking Happens

Tree shaking occurs automatically during:

  • Release builds:
    text
    flutter build apk --release
    ,
    text
    flutter build ios --release
  • Profile builds:
    text
    flutter build apk --profile
  • NOT in debug builds: Debug mode keeps all code for hot reload functionality

Best Practices for Tree Shaking

  1. Import Specific Components: Instead of importing entire libraries, import only what you need
  2. Avoid Dynamic Imports: Tree shaking works best with static imports
  3. Remove Unused Dependencies: Regularly audit your
    text
    pubspec.yaml
    for unused packages
  4. Use const Constructors: Helps with optimization during compilation
  5. Avoid Reflection: Heavy use of reflection can prevent effective tree shaking
dart
// ❌ Bad: Imports entire library
import 'package:my_package/my_package.dart';

// ✅ Good: Imports only what's needed
import 'package:my_package/specific_class.dart';

Checking Tree Shaking Results

You can analyze the impact of tree shaking by:

bash
# Build release version
flutter build apk --release

# Analyze bundle size
flutter build apk --analyze-size

Important: Tree shaking is most effective when combined with other optimization techniques like code splitting, lazy loading, and proper dependency management.

Documentation: Flutter Performance Best Practices