gskinnerTeam / flutter-universal-platform

A web-safe implementation of dart.io.Platforms. Helps avoid the "Unsupported operation: Platform._operatingSystem" runtime error.
MIT License
105 stars 27 forks source link

Please provide UniversalPlatform.operatingSystem #14

Closed giorgio79 closed 2 years ago

giorgio79 commented 3 years ago

Hello,

Love the package and the fact that it has lots of booleans. Could we get a detected operatingSystem output as well similar to dart.io?

SergeShkurko commented 2 years ago

Temporary solution:

extension UniversalPlatformGetType on UniversalPlatform {
  static UniversalPlatformType get type {
    if (UniversalPlatform.isMacOS) {
      return UniversalPlatformType.MacOS;
    } else if (UniversalPlatform.isWindows) {
      return UniversalPlatformType.Windows;
    } else if (UniversalPlatform.isLinux) {
      return UniversalPlatformType.Linux;
    } else if (UniversalPlatform.isAndroid) {
      return UniversalPlatformType.Android;
    } else if (UniversalPlatform.isIOS) {
      return UniversalPlatformType.IOS;
    } else if (UniversalPlatform.isFuchsia) {
      return UniversalPlatformType.Fuchsia;
    }

    return UniversalPlatformType.Web;
  }

  static String get operatingSystem {
    return type.operatingSystem;
  }
}

extension UniversalPlatformTypeToOperatingSystem on UniversalPlatformType {
  String get operatingSystem {
    switch (this) {
      case UniversalPlatformType.MacOS:
        return "macos";
      case UniversalPlatformType.Windows:
        return "windows";
      case UniversalPlatformType.Linux:
        return "linux";
      case UniversalPlatformType.Android:
        return "android";
      case UniversalPlatformType.IOS:
        return "ios";
      case UniversalPlatformType.Fuchsia:
        return "fuchsia";
      case UniversalPlatformType.Web:
      default:
        return "web";
    }
  }
}

// Usage:
print(
  UniversalPlatformGetType.operatingSystem
);