How do you detect the host platform from Dart code?

FlutterDartHost

Flutter Problem Overview


For UI that should differ slightly on iOS and Android, i.e. on different platforms, there must be a way to detect which one the app is running on, but I couldn't find it in the docs. What is it?

Flutter Solutions


Solution 1 - Flutter

import 'dart:io' show Platform;

if (Platform.isAndroid) {
  // Android-specific code
} else if (Platform.isIOS) {
  // iOS-specific code
}

All options include:

Platform.isAndroid
Platform.isFuchsia
Platform.isIOS
Platform.isLinux
Platform.isMacOS
Platform.isWindows

You can also detect if you are running on the web using kIsWeb, a global constant indicating if the application was compiled to run on the web:

import 'package:flutter/foundation.dart' show kIsWeb;

if (kIsWeb) {
  // running on the web!
} else {
  // NOT running on the web! You can check for additional platforms here.
}

Solution 2 - Flutter

Thanks to Collin, the final answer is:

bool isIOS = Theme.of(context).platform == TargetPlatform.iOS;

Solution 3 - Flutter

Although defaultTargetPlatform will work, I would suggest using Theme.of(context).targetPlatform. This enables testing of iOS behavior (because defaultTargetPlatform is always TargetPlatform.android in tests). It also allows ancestors of your widget to override its target platform by wrapping it in a Theme widget.

Solution 4 - Flutter

import 'dart:io' show Platform;  //at the top
    
String os = Platform.operatingSystem; //in your code
print(os);

Solution 5 - Flutter

It is simple just import the io library

import'dart:io' show Platform;
void main(){
if(Platform.isIOS){
  return someThing();
}else if(Platform.isAndroid){
  return otherThing();
}else if(Platform.isMacOS){
  return anotherThing();
}

or in very simple way

Platform.isIOS ? someThing() : anOther(),

Solution 6 - Flutter

if (Platform.isAndroid) {
  // Android-specific code/UI Component
} else if (Platform.isIOS) {
  // iOS-specific code/UI Component
}

Don't forget to import IO Library.

import 'dart:io';

If you are using import 'dart:html'; too in same file then you have to specify Platform definition as both libraries has definition of "Platform"

in that case use Platform Specific Code like Below:

import 'dart:io' as IO;
import 'dart:html';

if (IO.Platform.isAndroid) {
  // Android-specific code/UI Component
} else if (IO.Platform.isIOS) {
  // iOS-specific code/UI Component
}

If you are looking into Platform Integration properly I would Suggest Use Complete Example on Flutter site: https://flutter.dev/docs/development/platform-integration/platform-channels

Solution 7 - Flutter

You can do

defaultTargetPlatform == TargetPlatform.iOS
          ? kIOSTheme
          : kDefaultTheme,

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

Solution 8 - Flutter

Most "Flutter" answer is as follows:

import 'package:flutter/foundation.dart' show TargetPlatform;

//...

if(Theme.of(context).platform == TargetPlatform.android)
    //do sth for Android
else if(Theme.of(context).platform == TargetPlatform.iOS)
    //do sth else for iOS
else if(Theme.of(context).platform == TargetPlatform.fuchsia)
    //even do sth else for Fuchsia OS

Solution 9 - Flutter

for more simple way for web and app both.try this

import 'dart:io' show Platform;
import 'package:flutter/foundation.dart' show kIsWeb;


var platformName = '';
if (kIsWeb) {
  platformName = "Web";
} else {
  if (Platform.isAndroid) {
    platformName = "Android";
  } else if (Platform.isIOS) {
    platformName = "IOS";
  } else if (Platform.isFuchsia) {
    platformName = "Fuchsia";
  } else if (Platform.isLinux) {
    platformName = "Linux";
  } else if (Platform.isMacOS) {
    platformName = "MacOS";
  } else if (Platform.isWindows) {
    platformName = "Windows";
  }
}
print("platformName :- "+platformName.toString());

Solution 10 - Flutter

you can add this extension file to the project and call in any object

import 'dart:io';
import 'package:flutter/foundation.dart' show kIsWeb;

extension Target on Object {
  bool isAndroid() {
    return Platform.isAndroid;
  } 
  bool isIOS() {
    return Platform.isIOS;
  } 
  bool isLinux() {
  return Platform.isLinux;
  } 
  bool isWindows() {
  return Platform.isWindows; 
  }
  bool isMacOS() {
  return Platform.isMacOS; 
  }
  bool isWeb() {
  return kIsWeb; 
  }
  // ···
}

just import the file and call it

@override
  Widget build(BuildContext context) {
    return isAndroid()? Text("Android"):Text("Not Android");
  }

Solution 11 - Flutter

import 'dart:io' as io;

if(io.Platform.isAndroid){
 doSomething();
}else {
 doSomethingElse();
}

Solution 12 - Flutter

You can use Universal Platform package:

https://pub.dev/packages/universal_platform

import 'package:universal_platform/universal_platform.dart';

bool isIos = UniversalPlatform.isIOS;
bool isAndroid = UniversalPlatform.isAndroid;
bool isWeb = UniversalPlatform.isWeb;
print('iOS: $isIos');
print('Android: $isAndroid');
print('Web: $isWeb');

Solution 13 - Flutter

So here is a little utility I use to detect platforms and os to be reactive as appropriate.

import'dart:io' show Platform;
import 'package:flutter/foundation.dart' show kIsWeb;

class Util {

  os getPlatform() {
    if (kIsWeb) {
      return os.Web;
    } else if (Platform.isIOS) {
      return os.IOS;
    } else if (Platform.isAndroid) {
      return os.Android;
    } else if (Platform.isFuchsia) {
      return os.Fuchsia;
    } else if (Platform.isLinux) {
      return os.Linux;
    } else if (Platform.isMacOS) {
      return os.MacOS;
    } else if (Platform.isWindows) {
      return os.Windows;
    }
    return os.Unknown;
  }

  bool isWeb() {
    return (getPlatform()==os.Web);
  }

  bool isMobile() {
    os platform = getPlatform();
    return (platform == os.Android || platform == os.IOS || platform== os.Fuchsia);
  }

  bool isComputer() {
    os platform = getPlatform();
    return (platform == os.Linux || platform == os.MacOS || platform== os.Windows);
  }

}

enum os { Unknown, Web, Android, Fuchsia, IOS, Linux, MacOS, Windows }

Solution 14 - Flutter

Checking Host Platform in Dart.

import 'dart:io' as IO;

_checkingHostPlatform(){
    if(IO.Platform.isAndroid){
      //Execute code for android
    }else if(IO.Platform.isIOS){
      //Execute code for iOS
    }else{
      //Execute code for other platforms
    }
  }

Solution 15 - Flutter

If you just need a string for logging purposes, you can use Platform.operatingSystem, which returns the OS name as a lowercase string.

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

String _getPlatform() {
  if (kIsWeb) return 'web';
  return Platform.operatingSystem;
}

Solution 16 - Flutter

import 'dart:io' show Platform;

if (Platform.isAndroid) {
  // Android-specific code
} else if (Platform.isIOS) {
  // iOS-specific code
}else if (Platform.isFuchsia) {
  // Fuchsia-specific code
}else if (Platform.isLinux) {
  // Linux-specific code
}else if (Platform.isMacOS) {
  // MacOS-specific code
}else if (Platform.isWindows) {
  // Windows-specific code
}else if (Platform.isWindows) {
  // Windows-specific code
}

> for web

import 'package:flutter/foundation.dart' show kIsWeb;

if (kIsWeb) {
  // running on the web!
} else {
  // NOT running on the web! You can check for additional platforms here.
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGavinView Question on Stackoverflow
Solution 1 - FlutterWesty92View Answer on Stackoverflow
Solution 2 - FlutterGavinView Answer on Stackoverflow
Solution 3 - FlutterCollin JacksonView Answer on Stackoverflow
Solution 4 - FlutterC-SpydoView Answer on Stackoverflow
Solution 5 - FlutterOsama BuzdarView Answer on Stackoverflow
Solution 6 - FlutterSkandar MunirView Answer on Stackoverflow
Solution 7 - FlutterRémi RousseletView Answer on Stackoverflow
Solution 8 - FlutterUgurcan YildirimView Answer on Stackoverflow
Solution 9 - Fluttershirsh shuklaView Answer on Stackoverflow
Solution 10 - FlutterOmar MahmoudView Answer on Stackoverflow
Solution 11 - FlutterAnuran BarmanView Answer on Stackoverflow
Solution 12 - FlutterJ. Nuno Negrão MartinsView Answer on Stackoverflow
Solution 13 - FlutterhunterinoView Answer on Stackoverflow
Solution 14 - FlutterEmmanuel AmetepeeView Answer on Stackoverflow
Solution 15 - FlutterSuragchView Answer on Stackoverflow
Solution 16 - FlutterAnandh KrishnanView Answer on Stackoverflow