'Will dart remove unreachable code during compilation?

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?


Solution 1:[1]

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 code dependent on that boolean 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) (or 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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1