defaultTargetPlatform top-level property
The TargetPlatform that matches the platform on which the framework is currently executing.
This is the default value of ThemeData.platform (hence the name). Widgets
from the material library should use Theme.of to determine the current
platform for styling purposes, rather than using defaultTargetPlatform.
However, if there is widget behavior that depends on the actual underlying
platform, then depending on defaultTargetPlatform makes sense.
dart.io.Platform.environment
should be used directly only when it's
critical to actually know the current platform, without any overrides
possible (for example, when a system API is about to be called).
In a test environment, the platform returned is TargetPlatform.android regardless of the host platform. (Android was chosen because the tests were 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.
Implementation
//
// When adding support for a new platform (e.g. Windows Phone, macOS), first
// create a new value on the [TargetPlatform] enum, then add a rule for
// selecting that platform here.
//
// It would be incorrect to make a platform that isn't supported by
// [TargetPlatform] default to the behavior of another platform, because doing
// 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).
TargetPlatform get defaultTargetPlatform {
TargetPlatform result;
if (Platform.isIOS) {
result = TargetPlatform.iOS;
} else if (Platform.isAndroid) {
result = TargetPlatform.android;
} else if (Platform.isFuchsia) {
result = TargetPlatform.fuchsia;
}
assert(() {
if (Platform.environment.containsKey('FLUTTER_TEST'))
result = TargetPlatform.android;
return true;
}());
if (debugDefaultTargetPlatformOverride != null)
result = debugDefaultTargetPlatformOverride;
if (result == null) {
throw FlutterError(
'Unknown platform.\n'
'${Platform.operatingSystem} was not recognized as a target platform. '
'Consider updating the list of TargetPlatforms to include this platform.'
);
}
return result;
}