Home > front end >  Will dart remove unreachable code during compilation?
Will dart remove unreachable code during compilation?

Time:03-02

Suppose, I'd like to release two versions of an application:

  • Version one should be somehow limited and provide just a limited set of features
  • Version two may be a full version, which should include all features.

Q: In case I define a bool static property enableAllFeatures, will dart

  • remove all non-used properties,
  • remove all non-used methods and classes and
  • remove all non-reachable (e.g. Widget-creation) code,
  • remove all non-used plugins / frameworks?

CodePudding user response:

Flutter/Dart use tree shaking to reduce the final packet size which removes any unused code from the final build. This happens on the release version, not the debug compilation

CodePudding user response:

A static bool property would not allow code to be removed since such a property could be changed at runtime, and therefore there would be no way for the compiler to deduce that that code might be unreachable.

A const bool, on the other hand, would be a compile-time constant, and therefore the compiler could use that to identify unreachable code. Typically you'd use bool.fromEnvironment(name) (int.fromEnvironment or String.fromEnvironment) in a const context to conditionally enable code for different build types. See Can I use Custom Environment Variables in Flutter? for some more details.

  • Related