【问题标题】:Flutter. Future should wait for callback扑。未来应该等待回调
【发布时间】:2021-10-02 14:26:11
【问题描述】:

我正在 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 的新手。

【问题讨论】:

    标签: flutter huawei-mobile-services huawei-developers huawei-push-notification


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2017-01-22
      • 2020-08-11
      • 2016-04-20
      • 1970-01-01
      • 2021-03-19
      • 2019-12-19
      • 1970-01-01
      • 2021-06-30
      • 2015-02-04
      相关资源
      最近更新 更多