代码中检测宿主平台

代码中检测宿主平台

本文介绍了如何从 Dart 代码中检测宿主平台?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于在 iOSAndroid(即在不同平台)上应该略有不同的 UI,必须有一种检测应用程序在哪个应用程序上运行的方法,但我在文档中找不到它.它是什么?

For UI that should differ slightly on iOS and Android, i.e. on different platforms, there must be a way to detect which one the app is running on, but I couldn't find it in the docs. What is it?

推荐答案

import 'dart:io' show Platform;

if (Platform.isAndroid) {
  // Android-specific code
} else if (Platform.isIOS) {
  // iOS-specific code
}

所有选项包括:

Platform.isAndroid
Platform.isFuchsia
Platform.isIOS
Platform.isLinux
Platform.isMacOS
Platform.isWindows

您还可以使用 kIsWeb 检测您是否在网络上运行,这是一个全局常量,指示应用程序是否已编译为在网络上运行:

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.
}

08-06 15:40