website icon indicating copy to clipboard operation
website copied to clipboard

Add information about how platform-specific Dart code can be guarded so that it is appropriately tree-shaken in release applications

Open sstrickl opened this issue 8 months ago • 0 comments

What information needs to be added?

Would it be possible to say somewhere that code guarded with checks against defaultTargetPlatform, e.g.,

if (defaultTargetPlatform == TargetPlatform.windows) {
  /* Windows-specific code here */ 
}

or

switch (defaultTargetPlatform) {
  case TargetPlatform.windows:
    /* Windows-specific code here */
    break;
  ...
}

are appropriately tree-shaken in release builds of Flutter applications (and so dependencies used only in code guarded by such checks should also be tree-shaken, etc.)?

Where should this new content appear?

Currently it looks like the only examples of code using defaultTargetPlatform are in https://docs.flutter.dev/platform-integration/macos/platform-views and https://docs.flutter.dev/platform-integration/ios/platform-views, and https://docs.flutter.dev/platform-integration/platform-channels only mentions defaultTargetPlatform in a sidebar, so I'm unsure if those are the right places or not, or if this should be relegated to the defaultTargetPlatform/TargetPlatform API docs.

See https://github.com/dart-lang/sdk/issues/31969#issuecomment-2612806599 for the original request, and see https://github.com/flutter/flutter/issues/14233#issuecomment-2545426221 for more background. Let me know if there's anything I can do to help, or if there's some place I should file this instead. Thanks!

I would like to fix this problem.

  • [ ] I will try and fix this problem on docs.flutter.dev.

sstrickl avatar Feb 03 '25 12:02 sstrickl