Skip to content

[Feature] Add multi-platform safety#20

Open
austinn wants to merge 1 commit into
istornz:mainfrom
austinn:safe-platform-checking
Open

[Feature] Add multi-platform safety#20
austinn wants to merge 1 commit into
istornz:mainfrom
austinn:safe-platform-checking

Conversation

@austinn

@austinn austinn commented Jun 4, 2026

Copy link
Copy Markdown

Description

Addresses: #19

This PR resolves issues when using the gaimon package in multi-platform Flutter applications (e.g. Web, macOS, Windows, Linux).

Previously, the package would:

  1. Crash at runtime on Web with Unsupported operation: Platform._operatingSystem due to direct access to Platform.isAndroid (from dart:io).
  2. Throw MissingPluginException at runtime on non-mobile platforms (macOS, Windows, Linux) when invoking any haptic action due to unguarded MethodChannel invocations.

Solution

  • Introduced kIsWeb guards to safely short-circuit Platform checks on the Web.
  • Guarded all MethodChannel invocations (_channel.invokeMethod) to ensure they are only triggered on supported mobile platforms (Android and iOS).
  • Added try-catch safety to canSupportsHaptic to fallback to false in case of unexpected platform errors.
  • Allowed Flutter SDK native methods (e.g., HapticFeedback.selectionClick(), HapticFeedback.heavyImpact()) to run untouched as they are natively stubbed/handled safely on all platforms by Flutter.
  • Added a new unit test suite verifying that all calls run successfully without throwing exceptions on unsupported platforms (like macOS).

Documentation

Documented platform compatibility and fallback behavior in README.md

…b, macOS, Windows, Linux, and other desktop/mobile targets)

@istornz istornz left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for you PR, I let some little comments 👍

Comment thread lib/gaimon.dart
class Gaimon {
static const MethodChannel _channel = MethodChannel('gaimon');

static bool get _isAndroid => !kIsWeb && Platform.isAndroid;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is enough, same for iOS

Suggested change
static bool get _isAndroid => !kIsWeb && Platform.isAndroid;
static bool get _isAndroid => Platform.isAndroid;

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @istornz I'm not sure I'm following - web doesn't support the Platform API - so why wouldn't we add this before a native platform check?

Comment thread lib/gaimon.dart

static bool get _isAndroid => !kIsWeb && Platform.isAndroid;
static bool get _isIOS => !kIsWeb && Platform.isIOS;
static bool get _supportsMethodChannel => _isAndroid || _isIOS;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe adding the kIsWeb in this condition is better?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@istornz This package only supports Android and iOS, so I'm not sure why adding a kIsWeb would be useful here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants