|
1 | 1 | import 'dart:async'; |
2 | 2 | import 'dart:developer'; |
3 | 3 |
|
| 4 | +import 'package:flutter/material.dart'; |
4 | 5 | import 'package:flutter/services.dart'; |
5 | 6 | import 'package:flutter_overlay_window/src/models/overlay_position.dart'; |
6 | 7 | import 'package:flutter_overlay_window/src/overlay_config.dart'; |
@@ -95,9 +96,32 @@ class FlutterOverlayWindow { |
95 | 96 | return _res; |
96 | 97 | } |
97 | 98 |
|
98 | | - /// Broadcast data to and from overlay app |
99 | | - static Future shareData(dynamic data) async { |
100 | | - return await _overlayMessageChannel.send(data); |
| 99 | + /// Broadcast [data] to and from overlay app. |
| 100 | + /// |
| 101 | + /// If `true` is returned, it indicates that the [data] was sent. However, this doesn't mean |
| 102 | + /// that the [data] has already reached the listeners of the [overlayListener] stream. |
| 103 | + /// |
| 104 | + /// If `false` is returned, it indicates that the [data] was not sent. |
| 105 | + /// |
| 106 | + /// This method may return `false` under the following conditions: |
| 107 | + /// - The overlay is closed. |
| 108 | + /// - The application is detached from the activity, i.e. the application is closed. |
| 109 | + /// |
| 110 | + /// Returns `true` if the [data] was sent successfully, otherwise `false`. |
| 111 | + /// |
| 112 | + /// May return `null`, indicating a failure to send the [data] (This is unexpected behavior, |
| 113 | + /// and if encountered, indicates a bug within plugin implementation that should be addressed). |
| 114 | + static Future<bool?> shareData(dynamic data) async { |
| 115 | + final isSent = await _overlayMessageChannel.send(data); |
| 116 | + if (isSent == null) { |
| 117 | + debugPrintStack( |
| 118 | + stackTrace: StackTrace.current, |
| 119 | + label: "[FlutterOverlayWindow] ERROR: " |
| 120 | + "Failed to send the [data] using the [shareData] method. " |
| 121 | + "Message channel handler is not registered."); |
| 122 | + return null; |
| 123 | + } |
| 124 | + return isSent as bool; |
101 | 125 | } |
102 | 126 |
|
103 | 127 | /// Streams message shared between overlay and main app |
@@ -165,7 +189,7 @@ class FlutterOverlayWindow { |
165 | 189 | /// Dispose overlay stream. |
166 | 190 | /// |
167 | 191 | /// Once disposed, only a complete restart of the application will re-initialize the listener. |
168 | | - static void disposeOverlayListener() { |
169 | | - _controller.close(); |
| 192 | + static Future<dynamic> disposeOverlayListener() { |
| 193 | + return _controller.close(); |
170 | 194 | } |
171 | 195 | } |
0 commit comments