diff --git a/packages/flutter/lib/src/foundation/_platform_io.dart b/packages/flutter/lib/src/foundation/_platform_io.dart index d7750c08b20..7fd4f35ff7a 100644 --- a/packages/flutter/lib/src/foundation/_platform_io.dart +++ b/packages/flutter/lib/src/foundation/_platform_io.dart @@ -4,11 +4,13 @@ import 'dart:io'; import 'assertions.dart'; +import 'constants.dart'; import 'platform.dart' as platform; export 'platform.dart' show TargetPlatform; /// The dart:io implementation of [platform.defaultTargetPlatform]. +@pragma('vm:platform-const-if', !kDebugMode) platform.TargetPlatform get defaultTargetPlatform { platform.TargetPlatform? result; if (Platform.isAndroid) { @@ -30,7 +32,7 @@ platform.TargetPlatform get defaultTargetPlatform { } return true; }()); - if (platform.debugDefaultTargetPlatformOverride != null) { + if (kDebugMode && platform.debugDefaultTargetPlatformOverride != null) { result = platform.debugDefaultTargetPlatformOverride; } if (result == null) { diff --git a/packages/flutter/lib/src/foundation/platform.dart b/packages/flutter/lib/src/foundation/platform.dart index 60d90b22d77..b1905aea1a9 100644 --- a/packages/flutter/lib/src/foundation/platform.dart +++ b/packages/flutter/lib/src/foundation/platform.dart @@ -4,6 +4,8 @@ import '_platform_io.dart' if (dart.library.js_util) '_platform_web.dart' as platform; +import 'assertions.dart'; +import 'constants.dart'; /// The [TargetPlatform] that matches the platform on which the framework is /// currently executing. @@ -22,7 +24,7 @@ import '_platform_io.dart' /// originally written assuming Android-like behavior, and we added platform /// adaptations for iOS later). Tests can check iOS behavior by using the /// platform override APIs (such as [ThemeData.platform] in the material -/// library) or by setting [debugDefaultTargetPlatformOverride]. +/// library) or by setting [debugDefaultTargetPlatformOverride] in debug builds. /// /// Tests can also create specific platform tests by and adding a `variant:` /// argument to the test and using a [TargetPlatformVariant]. @@ -42,6 +44,7 @@ import '_platform_io.dart' // that would mean we'd be stuck with that platform forever emulating the other, // and we'd never be able to introduce dedicated behavior for that platform // (since doing so would be a big breaking change). +@pragma('vm:platform-const-if', !kDebugMode) TargetPlatform get defaultTargetPlatform => platform.defaultTargetPlatform; /// The platform that user interaction should adapt to target. @@ -76,7 +79,7 @@ enum TargetPlatform { windows, } -/// Override the [defaultTargetPlatform]. +/// Override the [defaultTargetPlatform] in debug builds. /// /// Setting this to null returns the [defaultTargetPlatform] to its original /// value (based on the actual current platform). @@ -94,5 +97,16 @@ enum TargetPlatform { /// certainly widgets to work assuming the presence of a system-wide back /// button, which will make those widgets unusable since iOS has no such button. /// -/// In general, therefore, this property should not be used in release builds. -TargetPlatform? debugDefaultTargetPlatformOverride; +/// Attempting to override this property in non-debug builds causes an error. +TargetPlatform? get debugDefaultTargetPlatformOverride => + _debugDefaultTargetPlatformOverride; + +set debugDefaultTargetPlatformOverride(TargetPlatform? value) { + if (!kDebugMode) { + throw FlutterError( + 'Cannot modify debugDefaultTargetPlatformOverride in non-debug builds.'); + } + _debugDefaultTargetPlatformOverride = value; +} + +TargetPlatform? _debugDefaultTargetPlatformOverride;