搜索插件、代码仓与 APP 分类

插件详情v1.1.0

universal_platform

dart:io.Platform 类的替代品,可在 Web、桌面和移动设备上使用。可轻松在视图/模型层进行平台检查。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
181万
喜欢数
313
Pub 分数
150

安装插件

flutter pub add universal_platform

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

universal_platform 包提供了一个跨平台的替代方案,用于 Dart 的 Platform 类,可在 Web、移动和桌面环境中实现一致的平台检测。它允许开发者在业务逻辑层(如视图和模型)中执行平台检查,而无需依赖特定于平台的实现。这确保了无论目标运行时环境如何,都能保持可靠的行为,简化了需要根据底层平台进行自适应的代码。

适合场景

  • 跨平台应用开发
  • 平台特定的 UI/行为逻辑
  • 跨平台共享业务逻辑
  • 在多个目标上进行测试
  • 避免使用平台特定的导入

主要特点

  • 统一平台检测
  • 适用于网页、Android、iOS、macOS、Windows、Linux
  • 无需条件导入
  • 类似于 dart:io.Platform 的简单 API
  • 支持 isAndroid、isIOS、isWeb 等。

适合对象

  • 面向多平台的开发者
  • 需要一致的平台检查的应用程序
  • 团队避免平台特定代码重复
  • 跨平台使用共享逻辑的项目
  • 需要可靠网络支持的用户

使用建议

  • 需要显式导入,而不是 dart:io.Platform
  • 可能无法涵盖所有特定平台的功能
  • 仅限基本平台识别
  • 并非 dart:io 功能的完整替代
  • 依赖外部依赖管理

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

Universal Platform - A Web-safe Platform class

pub package

Currently, if you include the dart.io.Platform anywhere in your code, your app will throw the following error on Web:

Unsupported operation: Platform._operatingSystem

With this plugin you can perform platform detection on all platforms, including Web, without errors.

🔨 Installation

dependencies:
  universal_platform: ^1.0.0+1
⚙ Import

Remove any usages of dart.io.Platform, and replace with this:

import 'package:universal_platform/universal_platform.dart';

🕹️ Usage

This acts as a drop-in replacement for dart.io.Platform, with a different name for improved clarity.

//This will explode on Web
bool isIos = Platform.isIOS;

//This will not :)
bool isIos = UniversalPlatform.isIOS;
bool isWeb = UniversalPlatform.isWeb;

🐞 Bugs/Requests

If you encounter any problems feel open an issue. If you feel the library is missing a feature, please raise a ticket on Github and we'll look into it. Pull request are also welcome.

📃 License

MIT License

所属分类

源码仓库

使用此插件的项目