Ad

Flutter. Future Should Wait For Callback

I am implementing Huawei push notifications in Flutter. Here: https://pub.dev/packages/huawei_push/example

I'd like to be able to have a way of calling this:

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

from my code and have my own class PushNotificationService() fetch the token and return it.

My problem is that for Huawei I need to implement some listeners inside PushNotificationsService() class.

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);

But how should I setup my getPushNotificationsToken() ? I need to return a future inside it, but how to handle that Future so that it's only "triggered" once one of my two listener callbacks are called ?!

I'm sure it's a basic solution, but I'm new to Flutter unfortunately.

Ad

Answer

Currently, the Flutter Push Kit plug-in can obtain token information only through event listening.

Ad
source: stackoverflow.com
Ad