How to check platform at runtime

6,306

Solution 1

I've search a bit on SO and tryed also to Google it but this scenario is not so well indexed and so I think my question and anser could help starting developing on Flutter.

If you have to check the OS or the Platform of your device at runtime you could use the Platform class of dart.io library.

import 'dart:io'

This way you could check in a way like that:

_openMap() async {
    // Android
    var url = 'geo:52.32,4.917';
    if (Platform.isIOS) {
      // iOS
      url = 'http://maps.apple.com/?ll=52.32,4.917';
    } else if (Platform.isWindows) {
      // TODO - something to do?
    }
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      throw 'Could not launch $url';
    }
  }

Instead if you also need some deep insight of the device you could use the dart device_info package.

There's a good example here.

This way you could also check not only the platform you are running on but also the specific version of OS (iOS 9, 10.3, 11.x, Lollipop, Jellybean, etc.) and many others device info.

UPDATE:

After Flutter Live 2018 --> Look at this gr8 youtube video for Platform Aware Widget and the best way to be compliant with Android and iOS UI from the same codebase.

Solution 2

The recommended way to get the current platform is by using Theme.

Theme.of(context).platform

This way, you could potentially override that value with a custom Theme at runtime and see immediately all the changes.

Solution 3

import 'dart:io'

String os = Platform.operatingSystem;

This is simple way to check platform and also allows a lot of other useful information about the device being used. Link to docs about the Platform class.

Share:
6,306
shadowsheep
Author by

shadowsheep

By day: Coding. By night: Sleeping (possibly). For Fun: Coding some (maybe new) language, sailing, playing sports, travelling, always looking for something new. I'm an Italian developer, VIM lover. I answer questions mainly for points but also 'cause I like it. Some people do crosswords, I like to solve problems instead. Many times I do not even know how to solve the issue in your question, but I study in order to figure it out. You may find me in some slack community workspaces or on IRC chats lurking to learn what I still don't know or trying to use some bleeding edge technology. What I hate the most? If I helped you and you don't bother even to upvote my answer. But hey! No hard feelings bro, just a "doh" >.< During my career I've used a plethora of framework, systems and languages. C#, Java, Objective-C, PHP, Javascript, SQL, Oracle (PL/SQL), MS SQL Server (Transact/SQL). In the last few years I'm working only on mobile applications (Android and iOS) and I tried Flutter. I've also experience in IT field so also Servers, Networking and Systems tuning/configuration is no stranger to me.

Updated on December 04, 2022

Comments

  • shadowsheep
    shadowsheep over 1 year

    How could I check the platform (Android/iOS) at runtime?

    I’d like to differ my flutter application behaviour if I'm on Android rather than I'm on iOS.

    Like this:

    _openMap() async {
        // Android
        var url = 'geo:52.32,4.917';
        if (/* i'm on iOS */) {
          url = 'http://maps.apple.com/?ll=52.32,4.917';
        }
        if (await canLaunch(url)) {
          await launch(url);
        } else {
          throw 'Could not launch $url';
        }
      }
    
  • shadowsheep
    shadowsheep about 6 years
    Oh, good to know! Thank you! It works, I've tested, and for a simple check it requires a bit more coding, but If you need runtime platform changing could be useful. This will improve my question for sure! Thank you +1!
  • Rémi Rousselet
    Rémi Rousselet about 6 years
    Anyway, remember that in flutter everything is a widget and that's not a joke. Platform check should be done with widgets too :p