startNotifications method

  1. @override
Future<void> startNotifications(
  1. StartNotificationsParams options,
  2. void callback(
    1. ReadResult result
    )
)
override

Implementation

@override
Future<void> startNotifications(StartNotificationsParams options,
    void Function(ReadResult result) callback) async {
  var optionsMap = options.getMap();
  optionsMap['callbackName'] = 'startNotifications';

  bool cancelStream = false;

  var brodcastStream =
      _eventChannel.receiveBroadcastStream(optionsMap).listen((event) {
    if (event == null || event["status"] == "success") {
      return;
    }
    if (event["status"] == "error") {
      cancelStream = true;
      return;
    }
    callback(event['value']);
    cancelStream = true;
  });

  Timer.periodic(const Duration(milliseconds: 500), (timer) {
    if (cancelStream) {
      brodcastStream.cancel();
      timer.cancel();
    }
  });
}