flutter / flutter/flutter

Make defaultTargetPlatform, debugDefaultTargetPlatformOverride a constant

Open
#167,587 0 comments 1 reaction 0 assignees View on GitHub
c: proposal framework P3 team-framework triaged-framework
Dominant language
Dart
Stars
179k
Forks
31.1k
PR merge metrics
PR metrics pending

Description

### Use case

I want to use different icons based on the platform in a `const` expression:

```dart
const Icon(defaultTargetPlatform == TargetPlatform.iOS ? Icons.add : CupertinoIcons.add)
```

But `defaultTargetPlatform` is a **[getter](https://api.flutter.dev/flutter/foundation/defaultTargetPlatform.html)**, not a `const`, so above code doesn’t work.

### My current workaround

Right now, I use a `--dart-define` for this:

```dart
const kIsCupertino = bool.fromEnvironment('USE_CUPERTINO')
const Icon(kIsCupertino == TargetPlatform.iOS ? Icons.add : CupertinoIcons.add)
```

### Proposal

Add const versions of:
- defaultTargetPlatform
- debugDefaultTargetPlatformOverride

This enables compile-time platform checks for things like icon or design system switching -> no context, better performance, simpler adaptive UI.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.