Skip to content

Commit 7ca4b7b

Browse files
authored
Mark defaultTargetPlatform as constant for non-debug non-web builds. (flutter#141105)
This PR adds the Dart VM `vm:platform-const-if` pragma introduced in dart-lang/sdk@57a1168875 to the `defaultTargetPlatform` property, allowing it to be computed as if it was a constant field in non-debug AOT builds. In particular, this means that platform-specific code executed conditionally based on this property can be tree-shaken in release builds. Note that this PR changes `defaultTargetPlatform` to only allow overriding via `debugDefaultTargetPlatformOverride` in debug builds, and makes it so that compilation throws an error if code assigns to`debugDefaultTargetPlatformOverride` in other build modes. Related issue: flutter#14233 ## Pre-launch Checklist - [X] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [X] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [X] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [X] I signed the [CLA]. - [X] I listed at least one issue that this PR fixes in the description above. - [X] I updated/added relevant documentation (doc comments with `///`). - [X] I added new tests to check the change I am making, or this PR is [test-exempt]. - [X] All existing and new tests are passing.
1 parent 9574d58 commit 7ca4b7b

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

packages/flutter/lib/src/foundation/_platform_io.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
import 'dart:io';
66
import 'assertions.dart';
7+
import 'constants.dart';
78
import 'platform.dart' as platform;
89

910
export 'platform.dart' show TargetPlatform;
1011

1112
/// The dart:io implementation of [platform.defaultTargetPlatform].
13+
@pragma('vm:platform-const-if', !kDebugMode)
1214
platform.TargetPlatform get defaultTargetPlatform {
1315
platform.TargetPlatform? result;
1416
if (Platform.isAndroid) {
@@ -30,7 +32,7 @@ platform.TargetPlatform get defaultTargetPlatform {
3032
}
3133
return true;
3234
}());
33-
if (platform.debugDefaultTargetPlatformOverride != null) {
35+
if (kDebugMode && platform.debugDefaultTargetPlatformOverride != null) {
3436
result = platform.debugDefaultTargetPlatformOverride;
3537
}
3638
if (result == null) {

packages/flutter/lib/src/foundation/platform.dart

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import '_platform_io.dart'
66
if (dart.library.js_util) '_platform_web.dart' as platform;
7+
import 'assertions.dart';
8+
import 'constants.dart';
79

810
/// The [TargetPlatform] that matches the platform on which the framework is
911
/// currently executing.
@@ -22,7 +24,7 @@ import '_platform_io.dart'
2224
/// originally written assuming Android-like behavior, and we added platform
2325
/// adaptations for iOS later). Tests can check iOS behavior by using the
2426
/// platform override APIs (such as [ThemeData.platform] in the material
25-
/// library) or by setting [debugDefaultTargetPlatformOverride].
27+
/// library) or by setting [debugDefaultTargetPlatformOverride] in debug builds.
2628
///
2729
/// Tests can also create specific platform tests by and adding a `variant:`
2830
/// argument to the test and using a [TargetPlatformVariant].
@@ -42,6 +44,7 @@ import '_platform_io.dart'
4244
// that would mean we'd be stuck with that platform forever emulating the other,
4345
// and we'd never be able to introduce dedicated behavior for that platform
4446
// (since doing so would be a big breaking change).
47+
@pragma('vm:platform-const-if', !kDebugMode)
4548
TargetPlatform get defaultTargetPlatform => platform.defaultTargetPlatform;
4649

4750
/// The platform that user interaction should adapt to target.
@@ -76,7 +79,7 @@ enum TargetPlatform {
7679
windows,
7780
}
7881

79-
/// Override the [defaultTargetPlatform].
82+
/// Override the [defaultTargetPlatform] in debug builds.
8083
///
8184
/// Setting this to null returns the [defaultTargetPlatform] to its original
8285
/// value (based on the actual current platform).
@@ -94,5 +97,16 @@ enum TargetPlatform {
9497
/// certainly widgets to work assuming the presence of a system-wide back
9598
/// button, which will make those widgets unusable since iOS has no such button.
9699
///
97-
/// In general, therefore, this property should not be used in release builds.
98-
TargetPlatform? debugDefaultTargetPlatformOverride;
100+
/// Attempting to override this property in non-debug builds causes an error.
101+
TargetPlatform? get debugDefaultTargetPlatformOverride =>
102+
_debugDefaultTargetPlatformOverride;
103+
104+
set debugDefaultTargetPlatformOverride(TargetPlatform? value) {
105+
if (!kDebugMode) {
106+
throw FlutterError(
107+
'Cannot modify debugDefaultTargetPlatformOverride in non-debug builds.');
108+
}
109+
_debugDefaultTargetPlatformOverride = value;
110+
}
111+
112+
TargetPlatform? _debugDefaultTargetPlatformOverride;

0 commit comments

Comments
 (0)