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.
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.
