Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you detect the host platform from Dart code?

Tags:

flutter

dart

People also ask

How do I find my dart platform?

Step 1: Add the import statement import 'package:flutter/foundation. dart'; to your file. The above code snippet checks if the current platform is Android or not. It first detects the current platform using defaultTargetPlatform and compares it with the values inside the TargetPlatform.

How do I know if my platform is web in Flutter?

You can check whether your Flutter app is running on a web browser by using the kIsWeb constant from the foundation library. You can find more information about the foundation library in Flutter's official documentation.


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.
}
  • Platform documentation: https://docs.flutter.io/flutter/dart-io/Platform-class.html
  • kIsWeb documentation: https://api.flutter.dev/flutter/foundation/kIsWeb-constant.html

Thanks to Collin, the final answer is:

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

import 'dart:io' show Platform;  //at the top

String os = Platform.operatingSystem; //in your code
print(os);

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.


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(),

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


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