搜尋套件、儲存庫與 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

所屬分類

原始碼儲存庫

使用此套件的專案