如何从Dart代码中检测主机平台?

Gav*_*vin 64 dart flutter

对于UI应该略有不同的iOSAndroid,必须有检测到您正在运行哪一个的方式,但在文档我找不到它.它是什么?

Wes*_*y92 145

import 'dart:io' show Platform;

if (Platform.isAndroid) {
  // Android-specific code
} else if (Platform.isIOS) {
  // iOS-specific code
}
Run Code Online (Sandbox Code Playgroud)

其他选择包括:

Platform.isFuchsia
Platform.isLinux
Platform.isMacOS
Platform.isWindows
kIsWeb // A global constant indicating if the application was compiled to run on the web
Run Code Online (Sandbox Code Playgroud)

文档:https://docs.flutter.io/flutter/dart-io/Platform-class.html

  • 由于其简单性和可读性,这应该是公认的答案。 (3认同)
  • 我已经更新了答案,以包括所需的导入。 (2认同)
  • 请注意,这不适用于 flutter web。由于依赖 dart:io 引发异常 (2认同)
  • @Srikanth,如果您在检查其他平台之前检查“kIsWeb”,就像我的答案所示,它会工作得很好。我刚刚在 Dartpad 网站上验证了这一点。 (2认同)

Gav*_*vin 48

感谢Collin,最终的答案是:

bool isIOS = Theme.of(context).platform == TargetPlatform.iOS;
Run Code Online (Sandbox Code Playgroud)

  • 留意这个解决方案。如果在“initstate()”中调用它,它将抛出异常,因为“Theme.of()”是继承的小部件,并且会监听更改。这不能在 ```initstate()``` 中完成,所以它会抛出异常 (5认同)
  • 只有这个答案与最新的Flutter框架保持同步.其余的答案没错,但defaultTargetPlatform似乎不再是框架的一部分 (2认同)

osa*_*dar 25

很简单,只需要导入io库

import'dart:io' show Platform;
void main(){
if(Platform.isIOS){
  return someThing();
}else if(Platform.isAndroid){
  return otherThing();
}else if(Platform.isMacOS){
  return anotherThing();
}
Run Code Online (Sandbox Code Playgroud)

或以非常简单的方式

Platform.isIOS ? someThing() : anOther(),
Run Code Online (Sandbox Code Playgroud)

  • 这是正确的答案。您只需从代码中删除任何 dart:HTML 导入,因为它会导致错误。 (2认同)

Ska*_*nir 19

if (Platform.isAndroid) {
  // Android-specific code/UI Component
} else if (Platform.isIOS) {
  // iOS-specific code/UI Component
}
Run Code Online (Sandbox Code Playgroud)

不要忘记导入 IO 库。

import 'dart:io';
Run Code Online (Sandbox Code Playgroud)

如果您import 'dart:html';在同一个文件中使用太多,那么您必须指定平台定义,因为两个库都有“平台”的定义

在这种情况下,请使用平台特定代码,如下所示:

import 'dart:io' as IO;
import 'dart:html';

if (IO.Platform.isAndroid) {
  // Android-specific code/UI Component
} else if (IO.Platform.isIOS) {
  // iOS-specific code/UI Component
}
Run Code Online (Sandbox Code Playgroud)

如果您正在正确研究平台集成,我建议在 Flutter 站点上使用完整示例:https : //flutter.dev/docs/development/platform-integration/platform-channels


Col*_*son 18

虽然defaultTargetPlatform会起作用,但我建议使用Theme.of(context).targetPlatform.这样可以测试iOS行为(因为defaultTargetPlatform它总是TargetPlatform.android在测试中).它还允许窗口小部件的祖先通过将其包装在Theme窗口小部件中来覆盖其目标平台.

  • `Platform.isIOS`与`defaultTargetPlatform`有同样的问题.它在测试中不起作用,不能被`Theme`小部件覆盖. (3认同)
  • 现在您可以使用 debugDefaultTargetPlatformOverride 覆盖单元测试中的 defaultTargetPlatform https://api.flutter.dev/flutter/foundation/debugDefaultTargetPlatformOverride.html (2认同)

shi*_*kla 12

对于网络和应用程序两者的更简单的方法。试试这个

import 'dart:io' show Platform;
import 'package:flutter/foundation.dart' show kIsWeb;


var platformName = '';
if (kIsWeb) {
  platformName = "Web";
} else {
  if (Platform.isAndroid) {
    platformName = "Android";
  } else if (Platform.isIOS) {
    platformName = "IOS";
  } else if (Platform.isFuchsia) {
    platformName = "Fuchsia";
  } else if (Platform.isLinux) {
    platformName = "Linux";
  } else if (Platform.isMacOS) {
    platformName = "MacOS";
  } else if (Platform.isWindows) {
    platformName = "Windows";
  }
}
print("platformName :- "+platformName.toString());
Run Code Online (Sandbox Code Playgroud)


Oma*_*oud 10

您可以将此扩展文件(platform_ext.dart)添加到项目中并调用任何对象

\n
import 'dart:io';\nimport 'package:flutter/foundation.dart' show kIsWeb;\n\nextension Target on Object {\n  bool isAndroid() {\n    return Platform.isAndroid;\n  } \n  bool isIOS() {\n    return Platform.isIOS;\n  } \n  bool isLinux() {\n  return Platform.isLinux;\n  } \n  bool isWindows() {\n  return Platform.isWindows; \n  }\n  bool isMacOS() {\n  return Platform.isMacOS; \n  }\n  bool isWeb() {\n  return kIsWeb; \n  }\n  // \xc2\xb7\xc2\xb7\xc2\xb7\n}\n
Run Code Online (Sandbox Code Playgroud)\n

只需导入文件并调用它

\n
   import 'platform_ext.dart';\n        ....\n        @override\n          Widget build(BuildContext context) {\n            return isAndroid()? Text("Android"):Text("Not Android");\n          }\n
Run Code Online (Sandbox Code Playgroud)\n

  • 这实在是太过分了。为什么每个物体都知道平台?虽然它很方便,但现在你的 String 对象将知道它是什么平台,这很糟糕。将其保存在单独的类中,当你需要此信息时可以实例化。 (3认同)

小智 9

import 'dart:io' show Platform;  //at the top

String os = Platform.operatingSystem; //in your code
print(os);
Run Code Online (Sandbox Code Playgroud)

  • 请注意,这不适用于 flutter web。由于依赖 dart:io 引发异常 (3认同)

Rém*_*let 7

你可以做

defaultTargetPlatform == TargetPlatform.iOS
          ? kIOSTheme
          : kDefaultTheme,
Run Code Online (Sandbox Code Playgroud)

import 'package:flutter/foundation.dart';


Ugu*_*rim 7

多数“ Flutter”答案如下:

import 'package:flutter/foundation.dart' show TargetPlatform;

//...

if(Theme.of(context).platform == TargetPlatform.android)
    //do sth for Android
else if(Theme.of(context).platform == TargetPlatform.iOS)
    //do sth else for iOS
else if(Theme.of(context).platform == TargetPlatform.fuchsia)
    //even do sth else for Fuchsia OS
Run Code Online (Sandbox Code Playgroud)


小智 7

import 'dart:io' as io;

if(io.Platform.isAndroid){
 doSomething();
}else {
 doSomethingElse();
}
Run Code Online (Sandbox Code Playgroud)


use*_*448 6

因此,这里有一个小实用程序,我用它来检测适当的反应平台和操作系统。

import'dart:io' show Platform;
import 'package:flutter/foundation.dart' show kIsWeb;

class Util {

  os getPlatform() {
    if (kIsWeb) {
      return os.Web;
    } else if (Platform.isIOS) {
      return os.IOS;
    } else if (Platform.isAndroid) {
      return os.Android;
    } else if (Platform.isFuchsia) {
      return os.Fuchsia;
    } else if (Platform.isLinux) {
      return os.Linux;
    } else if (Platform.isMacOS) {
      return os.MacOS;
    } else if (Platform.isWindows) {
      return os.Windows;
    }
    return os.Unknown;
  }

  bool isWeb() {
    return (getPlatform()==os.Web);
  }

  bool isMobile() {
    os platform = getPlatform();
    return (platform == os.Android || platform == os.IOS || platform== os.Fuchsia);
  }

  bool isComputer() {
    os platform = getPlatform();
    return (platform == os.Linux || platform == os.MacOS || platform== os.Windows);
  }

}

enum os { Unknown, Web, Android, Fuchsia, IOS, Linux, MacOS, Windows }

Run Code Online (Sandbox Code Playgroud)


小智 5

您可以使用通用平台包:

https://pub.dev/packages/universal_platform

import 'package:universal_platform/universal_platform.dart';

bool isIos = UniversalPlatform.isIOS;
bool isAndroid = UniversalPlatform.isAndroid;
bool isWeb = UniversalPlatform.isWeb;
print('iOS: $isIos');
print('Android: $isAndroid');
print('Web: $isWeb');
Run Code Online (Sandbox Code Playgroud)

  • universal_platform 只是在底层使用 Platform.isWindows、Platform.isAndroid 等。 (2认同)

Ana*_*nan 5

import 'dart:io' show Platform;

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.isMacOS) {
  // MacOS-specific code
}else if (Platform.isWindows) {
  // Windows-specific code
}else if (Platform.isWindows) {
  // Windows-specific code
}
Run Code Online (Sandbox Code Playgroud)

用于网络

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.
}
Run Code Online (Sandbox Code Playgroud)