Hey there 😊

If you like what you read, feel free to …

🥗Buy me a salad
Share this 💬

How to detect what platform a Flutter app is running on

How to detect what platform a Flutter app is running on

Android? iOS? Web? MacOS? – With the growing number of supported build platforms, Flutter increases its value for developers who like to have only a single code base.
Also, this creates the necessity to make design decisions based on the platform it’s running on because the underlying APIs to access the hardware can vary.

Just to name a few things that are (not necessarily) available on the web platform:

  • Camera
  • Vibration
  • Push-Notifications
  • Acceleration sensor
  • Microphone

Flutter provides two different APIs that enables the caller to get to know more about the current platform: the kIsWeb constant that is part of the foundation library and the Platform class being part of the platform library.

The aim I have is to implement a class that provides an API that abstracts from these two quite low-level libraries. So let’s implement a class called PlatformInfo that has the public methods getCurrentPlatformType, isWeb, isAppOS and isDesktop. The first one should return an enum that is defined by us.

 1import 'dart:io';
 2import 'package:flutter/foundation.dart' show kIsWeb;
 3
 4class PlatformInfo {
 5  bool isDesktopOS() {
 6    return Platform.isMacOS || Platform.isLinux || Platform.isWindows;
 7  }
 8
 9  bool isAppOS() {
10    return Platform.isIOS || Platform.isAndroid;
11  }
12
13  bool isWeb() {
14    return kIsWeb;
15  }
16
17  PlatformType getCurrentPlatformType() {
18    if (kIsWeb) {
19      return PlatformType.Web;
20    }
21
22    if (Platform.isMacOS) {
23      return PlatformType.MacOS;
24    }
25
26    if (Platform.isFuchsia) {
27      return PlatformType.Fuchsia;
28    }
29
30    if (Platform.isLinux) {
31      return PlatformType.Linux;
32    }
33
34    if (Platform.isWindows) {
35      return PlatformType.Windows;
36    }
37
38    if (Platform.isIOS) {
39      return PlatformType.iOS;
40    }
41
42    if (Platform.isAndroid) {
43      return PlatformType.Android;
44    }
45
46    return PlatformType.Unknown;
47  }
48}
49
50enum PlatformType {
51  Web,
52  iOS,
53  Android,
54  MacOS,
55  Fuchsia,
56  Linux,
57  Windows,
58  Unknown
59}

This gives us an easy possibility to make decisions based on the OS or the group of underlying platforms:

1Text _getMicrophoneSection() {
2  if (PlatformInfo.isAppOS()) {
3    return Text('Please tap the microphone button and then permit the usage');
4  }
5
6  return Text('If you have a microphone available, please turn it on');
7}

The great thing about this class is: whenever Flutter enlarges its support portfolio, we can just extend it by adding a new enum member and an if-branch in the getCurrentPlatformType() method.
Until we’ve done that, nothing will break because we have a fallback enum member called PlatformType.Unknown.

Comments (1) ✍️

Anton Maurovic

Nice! There’s a minor error in isAppOS(): Replace “isMacOS” with “isIOS”.
Reply to Anton Maurovic

Comment this 🤌

You are replying to 's commentRemove reference