This tutorial shows you how to get the current platform that runs a Flutter application.
A Flutter application can run on different platforms. Currently, Flutter supports Android, iOS, Fuchsia, Linux, Windows, and macOS. Sometimes, it's necessary to know the current platform where the application runs in order to have a different behavior for a specific platform. Below are the examples of how to do it.
Using dart:io
Library
The dart:io
library has a class named Platform
which has some static methods to check whether the application runs on a specific platform. The methods are:
Platform.isAndroid
Platform.isIOS
Platform.isFuchsia
Platform.isLinux
Platform.isWindows
Platform.isMacOS
As you can see from the list above, there is no method for detecting whether the application runs on a web browser. In addition, calling any of the dart:io
's method above will cause Error: Unsupported operation: Platform._operatingSystem
on the web platform. To check whether the application runs on the web platform, you can use kIsWeb
constant from Flutter foundation
library. The value will be true if the application was compiled to run on the web.
import 'package:flutter/foundation.dart' show kIsWeb;
Below is the code example to check the current platform where the application runs.
if (kIsWeb) {
// Web-specific code
} else 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.isWindows) {
// Windows-specific code
} else if (Platform.isMacOS) {
// macOS-specific code
}
The Platform
class also has a static property operatingSystem
which returns the name of the operating system as a String. The possible values are:
- android
- fuchsia
- ios
- linux
- macos
- windows
It doesn't support the web platform as well, so you need a special handling for the web platform. However, it's not recommended to use the operatingSystem
property to differentiate the logic for a platform because the values may change over time.
if (kIsWeb) {
print('Web');
} else {
print(Platform.operatingSystem);
}
Using Flutter foundation
Library
Another alternative is using Flutter foundation
library. It has a property named defaultTargetPlatform
whose value is the TargetPlatform
matching the platform on which the framework is executing. If you look at the implementation of the defaultTargetPlatform
getter, actually it also uses the methods of dart:io
(Platform.isAndroid
, Platform.isIOS
, etc.) to return the TargetPlatform
value.
The defaultTargetPlatform
can be accessed by importing Flutter foundation
library. You may also need to import kIsWeb
constant for detecting whether the application runs on a web browser.
import 'package:flutter/foundation.dart' show defaultTargetPlatform, kIsWeb;
Below is the example for detecting the platform based on defaultTargetPlatform
value.
if (kIsWeb) {
// Web-specific code
} else if (defaultTargetPlatform == TargetPlatform.android) {
// Android-specific code
} else if (defaultTargetPlatform == TargetPlatform.iOS) {
// iOS-specific code
} else if (defaultTargetPlatform == TargetPlatform.fuchsia) {
// Fuchsia-specific code
} else if (defaultTargetPlatform == TargetPlatform.linux) {
// Linux-specific code
} else if (defaultTargetPlatform == TargetPlatform.windows) {
// Windows-specific code
} else if (defaultTargetPlatform == TargetPlatform.macOS) {
// macOS-specific code
}
Using ThemeData
The above methods have some limitations. In testing, the defaultTargetPlatform
is always TargetPlatform.android
. In addition, the ancestor widgets are unable to override the target platform. As a solution, you can get the platform value from the ThemeData
.
Theme.of(context).targetPlatform
You need to call it inside the build
method. Calling the code inside initState
may cause LateInitializationError
because the platform field has not been initialized.
Full Code
import 'dart:io' show Platform;
import 'package:flutter/foundation.dart' show defaultTargetPlatform, kIsWeb;
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: 'Woolha.com Flutter Tutorial',
home: CheckPlatformExample(),
);
}
}
class CheckPlatformExample extends StatelessWidget {
const CheckPlatformExample({Key? key}) : super(key: key);
String? _getPlatformUsingDartIo() {
String? platform;
if (kIsWeb) {
platform = 'Web';
} else if (Platform.isAndroid) {
platform = 'Android';
} else if (Platform.isIOS) {
platform = 'iOS';
} else if (Platform.isFuchsia) {
platform = 'Fuchsia';
} else if (Platform.isLinux) {
platform = 'Linux';
} else if (Platform.isWindows) {
platform = 'Windows';
} else if (Platform.isMacOS) {
platform = 'macOS';
}
return platform;
}
String? _getPlatformUsingFlutterFoundation() {
String? platform;
if (kIsWeb) {
platform = 'Web';
} else if (defaultTargetPlatform == TargetPlatform.android) {
platform = 'Android';
} else if (defaultTargetPlatform == TargetPlatform.iOS) {
platform = 'iOS';
} else if (defaultTargetPlatform == TargetPlatform.fuchsia) {
platform = 'Fuchsia';
} else if (defaultTargetPlatform == TargetPlatform.linux) {
platform = 'Linux';
} else if (defaultTargetPlatform == TargetPlatform.windows) {
platform = 'Windows';
} else if (defaultTargetPlatform == TargetPlatform.macOS) {
platform = 'macOS';
}
return platform;
}
String? _getPlatformUsingThemeData(BuildContext context) {
String? platform;
if (kIsWeb) {
platform = 'Web';
} else if (Theme.of(context).platform == TargetPlatform.android) {
platform = 'Android';
} else if (Theme.of(context).platform == TargetPlatform.iOS) {
platform = 'iOS';
} else if (Theme.of(context).platform == TargetPlatform.fuchsia) {
platform = 'Fuchsia';
} else if (Theme.of(context).platform == TargetPlatform.linux) {
platform = 'Linux';
} else if (Theme.of(context).platform == TargetPlatform.windows) {
platform = 'Windows';
} else if (Theme.of(context).platform == TargetPlatform.macOS) {
platform = 'macOS';
}
return platform;
}
String? _getOperatingSystem() {
String? operatingSystem;
if (kIsWeb) {
operatingSystem = 'Web';
} else {
operatingSystem = Platform.operatingSystem;
}
return operatingSystem;
}
@override
Widget build(BuildContext context) {
// final String? platform = _getPlatformUsingDartIo();
// final String? platform = _getPlatformUsingFlutterFoundation();
final String? platform = _getPlatformUsingThemeData(context);
final String? operatingSystem = _getOperatingSystem();
return Scaffold(
appBar: AppBar(
title: const Text('Woolha.com Flutter Tutorial'),
backgroundColor: Colors.teal,
),
body: SizedBox(
width: double.infinity,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Platform: ${platform ?? '-'}'),
Text('Operating system: ${operatingSystem ?? '-'}'),
],
),
),
);
}
}
Summary
You can detect the platform where a Flutter application runs by using dart:io
library's Package
class, Flutter foundation
library's defaultTargetPlatform
, or ThemeData
's platform
. Keep in mind that the above methods can only detect the platform on which the application is designed to run. For example, if you run the application on an Android emulator on a Windows computer, the platform will be Android instead of Windows.
You can also read about: