Flutter 查询当前运行的平台的方法

3 min read

可以使用dart.io.Platform类获取当前运行的平台信息。

例如,可以使用以下代码获取当前运行的平台的名称:

import 'dart:io';

void main() {
  if (Platform.isAndroid) {
    print('Running on Android');
  } else if (Platform.isIOS) {
    print('Running on iOS');
  } else if (Platform.isMacOS) {
    print('Running on macOS');
  } else if (Platform.isWindows) {
    print('Running on Windows');
  } else if (Platform.isLinux) {
    print('Running on Linux');
  } else {
    print('Running on other platform');
  }
}

此代码会输出当前运行的平台的名称,如“Running on macOS”、“Running on Linux”等。