pubnub / dart

PubNub Dart SDK
Other
28 stars 15 forks source link

The getter 'messages' isn't defined for the class 'Future<Subscription>'. #7

Closed trumanleung closed 4 years ago

trumanleung commented 4 years ago

After subscribing to a channel, the Future doesn't have a messages stream. Here's my code. pubnubChannel is a string with the name of the channel.

PubNub pubnub;
Future<Subscription> pubnubsub;
pubnub = PubNub(defaultKeyset: Keyset(subscribeKey: config.pubNubSubscribeKey));
pubnubsub = pubnub.subscribe(channels: { pubnubChannel });
pubnubsub.messages.listen((envelope) {
   print(`${envelope.uuid} sent a message: ${envelope.payload}`);
});

I'm getting this error: The getter 'messages' isn't defined for the class 'Future'.

are commented 4 years ago

Hi! Thanks for reporting an issue!

This is intended behaviour. pubnub.subscribe returns a Future<Subscription> and you need to use await to unwrap the Subscription from the future.

Take a look at this article: https://dart.dev/codelabs/async-await

trumanleung commented 4 years ago

Thank you. I added await before the pubnub.subscribe call and it works.