Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions lib/flutter_window_close.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class FlutterWindowClose {
static MethodChannel? _notificationChannel;
static const MethodChannel _channel = MethodChannel('flutter_window_close');

static UnsupportedError _webUnsupported() => UnsupportedError('This method must not be called on the web.');
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

To maintain consistency and provide a uniform way of handling platform-specific errors, consider adding a helper for non-web unsupported errors as well. This ensures that all platform-specific checks follow the same pattern and use consistent phrasing across the library.

Suggested change
static UnsupportedError _webUnsupported() => UnsupportedError('This method must not be called on the web.');
static UnsupportedError _webUnsupported() => UnsupportedError('This method must not be called on the web.');
static UnsupportedError _nonWebUnsupported() => UnsupportedError('This method must not be called on non-web platforms.');

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.

Currently, there is only one method that is web-only, so creating this utility is not needed.

The purpose of the introduced _webUnsupported() is to be more DRY.

_nonWebUnsupported()

What problem is this trying to solve?


static Future<void> _initIfRequired() async {
if (_notificationChannel != null) {
return;
Expand Down Expand Up @@ -77,7 +79,7 @@ class FlutterWindowClose {
/// The method does not support Flutter Web.
static Future<void> setWindowShouldCloseHandler(
Future<bool> Function()? handler) async {
if (kIsWeb) throw Exception('The method does not work in Flutter Web.');
if (kIsWeb) throw _webUnsupported();
_onWindowShouldClose = handler;
await _initIfRequired();
}
Expand All @@ -90,21 +92,21 @@ class FlutterWindowClose {
/// - On Linux, it calls [gtk_window_close](https://gnome.pages.gitlab.gnome.org/gtk/gtk4/method.Window.close.html)
/// - The method does not support Flutter Web.
static Future<void> closeWindow() async {
if (kIsWeb) throw Exception('The method does not work in Flutter Web.');
if (kIsWeb) throw _webUnsupported();
await _initIfRequired();
await _channel.invokeMethod('closeWindow');
}

static Future<void> destroyWindow() async {
if (kIsWeb) throw Exception('The method does not work in Flutter Web.');
if (kIsWeb) throw _webUnsupported();
await _initIfRequired();
await _channel.invokeMethod('destroyWindow');
}

/// Sets a return value when the current window or tab is being closed
/// when your app is running in Flutter Web.
static Future<void> setWebReturnValue(String? returnValue) async {
if (!kIsWeb) throw Exception('The method only works in Flutter Web.');
if (!kIsWeb) throw UnsupportedError('The method must not be called on non-web platforms.');
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Use the suggested _nonWebUnsupported() helper here for consistency with the other platform-specific checks in this class. This also aligns the error message phrasing ("This method" vs "The method") with the rest of the API.

Suggested change
if (!kIsWeb) throw UnsupportedError('The method must not be called on non-web platforms.');
if (!kIsWeb) throw _nonWebUnsupported();

await _channel.invokeMethod('setWebReturnValue', returnValue);
}
}