首页 > 解决方案 > 扑。未来应该等待回调

问题描述

我正在 Flutter 中实现华为推送通知。这里:https ://pub.dev/packages/huawei_push/example

我希望能够有一种调用方式:

String? token = await PushNotificationsService().getPushNotificationsToken();

从我的代码中,让我自己的类PushNotificationService()获取令牌并返回它。

我的问题是,对于华为,我需要在PushNotificationsService()课堂上实现一​​些监听器。

void _onTokenEvent(String event) {
    _token = event;
    showResult("TokenEvent", _token);
  }

  void _onTokenError(Object error) {
    PlatformException e = error as PlatformException;
    showResult("TokenErrorEvent", e.message!);
  }

...

Push.getTokenStream.listen(_onTokenEvent, onError: _onTokenError);

但是我应该如何设置我的getPushNotificationsToken()?我需要在其中返回一个未来,但是如何处理它Future以便只有在我的两个侦听器回调之一被调用时才“触发”它?!

我确信这是一个基本的解决方案,但不幸的是我是 Flutter 的新手。

标签: flutterhuawei-mobile-serviceshuawei-developershuawei-push-notification

解决方案


目前Flutter Push Kit插件只能通过事件监听获取token信息。


推荐阅读