如何在运行时检查平台 [英] How to check platform at runtime

查看:118
本文介绍了如何在运行时检查平台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在运行时检查平台(Android/iOS)?

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

如果我使用的是Android,而不是iOS,我希望改变Flutter应用程序的行为.

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

赞:

_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';
    }
  }

推荐答案

我在SO上进行了一些搜索,还尝试对Google进行了搜索,但是这种情况的索引不够好,所以我认为我的问题和分析服务可能会帮助开始在Flutter上开发.

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';
    }
  }

相反,如果您还需要深入了解设备,可以使用 dart device_info package

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

此处是一个很好的例子.

通过这种方式,您不仅可以检查正在运行的平台,还可以检查特定版本的OS(iOS 9, 10.3, 11.x, Lollipop, Jellybean等)以及许多其他设备信息.

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.

更新:

Flutter Live 2018之后->观看此gr8 youtube 视频,了解平台感知小部件以及实现此功能的最佳方法与同一代码库的Android和iOS UI兼容.

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.

这篇关于如何在运行时检查平台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆