|
| 1 | +import 'package:flutter/foundation.dart'; |
| 2 | + |
| 3 | +import 'services/base_service.dart'; |
| 4 | +import 'services/platform_channel_service.dart'; |
| 5 | + |
| 6 | +BasePlatformService get service { |
| 7 | + return PlatformChannelService(); |
| 8 | +} |
| 9 | + |
| 10 | +abstract class PlatformService { |
| 11 | + static final BasePlatformService _service = service; |
| 12 | + |
| 13 | + /// Invokes a [method] with or without [arguments] present at the native side. |
| 14 | + /// |
| 15 | + /// Returns a [Future] which completes to one of the following: |
| 16 | + /// |
| 17 | + /// * on successful invocation, a result (possibly null), |
| 18 | + /// * if the invocation failed in the platform plugin, a [PlatformException], |
| 19 | + /// * if the method has not been implemented by a platform plugin, a [MissingPluginException]. |
| 20 | + /// |
| 21 | + /// Example: |
| 22 | + /// ```dart |
| 23 | + /// final int sum = await PlatformService.invokeMethod<int>('getSum', {'a': 10, 'b': 20}); |
| 24 | + /// print(sum); // 30 |
| 25 | + /// ``` |
| 26 | + static Future<T?> invokeMethod<T>(String method, [dynamic arguments]) { |
| 27 | + return _service.invokeMethod<T>(method, arguments); |
| 28 | + } |
| 29 | + |
| 30 | + /// Sets up a [Stream] using the [eventChannelName] with or without [argumetns]. |
| 31 | + /// |
| 32 | + /// Returns a broadcast [Stream] which emits events to listeners as follows: |
| 33 | + /// |
| 34 | + /// * for every successfull event, a decoded data event (possibly null) is received from the |
| 35 | + /// platform plugin; |
| 36 | + /// * for every error event, an error event containing a [PlatformException] |
| 37 | + /// received from the platform plugin. |
| 38 | + /// |
| 39 | + /// When a stream is activated or deactivated, errors that happen are reported using the |
| 40 | + /// [FlutterError] capability. Only when the number of stream listeners increases from 0 to 1 does |
| 41 | + /// the stream become active. Deactivation of the stream only occurs when the number of stream |
| 42 | + /// listeners drops to zero. |
| 43 | + /// |
| 44 | + /// Example: |
| 45 | + /// ```dart |
| 46 | + /// final myStream = PlatformService.getEventStream('com.example.app/battery'); |
| 47 | + /// ``` |
| 48 | + static Stream<dynamic> getEventStream(String eventChannelName, [dynamic arguments]) { |
| 49 | + return _service.getEventStream(eventChannelName, arguments); |
| 50 | + } |
| 51 | +} |
0 commit comments