---
title: "How do you detect the platform your Flutter app is running on?"
description: "Use defaultTargetPlatform and kIsWeb to branch by platform without importing dart:io, which does not work on the web."
url: "https://bugsee.com/answers/flutter/detect-the-host-platform/"
---

# How do you detect the platform your Flutter app is running on?

Use `kIsWeb` to detect the web, and `defaultTargetPlatform` for mobile and desktop. Avoid `Platform.isAndroid` from `dart:io` in shared code, because it throws on the web.

```dart
import 'package:flutter/foundation.dart';

String platformName() {
  if (kIsWeb) return 'web';
  switch (defaultTargetPlatform) {
    case TargetPlatform.android:
      return 'android';
    case TargetPlatform.iOS:
      return 'ios';
    case TargetPlatform.macOS:
    case TargetPlatform.windows:
    case TargetPlatform.linux:
      return 'desktop';
    default:
      return 'other';
  }
}
```

Check `kIsWeb` first. On the web, `defaultTargetPlatform` reports the browser's operating system, which is useful for matching native look and feel but does not mean the code is running natively. If only the screen size matters, prefer `MediaQuery` or `LayoutBuilder` over platform checks.

## Related questions

- [How do you fix "A RenderFlex overflowed by pixels" in Flutter?](https://bugsee.com/answers/flutter/fix-renderflex-overflowed/)
