对于“ pubsub.stream.listen(print,onDone:(){print('done')}))”,则“ onDone:”永远无效

时间:2019-01-06 11:05:01

标签: dart flutter

当从redis收到消息时,onDone:(){print('done')}从未起作用。

import 'package:dartis/dartis.dart' as redis show PubSub;

void main() async {
  final pubsub = await redis.PubSub.connect<String, 
String>('redis://localhost:6379');

// Subscribe to some channels and patterns
  pubsub
    ..subscribe(channel: 'dev.dart')
    ..psubscribe(pattern: 'message');

// Listen for server replies
  pubsub.stream.listen(print, onDone: (){
    print('done');
  });

我想要的只是运行一个自定义函数,当收到新消息时,该消息将从redis接收到。 SOS!

1 个答案:

答案 0 :(得分:1)

在此片段中

pubsub.stream.listen(print, onDone: (){
  print('done');
});

print的首次出现被称为撕裂,等同于:

pubsub.stream.listen((event) => print(event), onDone: (){
  print('done');
});

您应该看到每个事件都已打印。

Stream发出MessageEvent s-请参见documentationMessageEvent具有属性channelmessage

编写自己的函数来处理事件(例如)

void handleEvent(MessageEvent<String, String> event) => print(event.message);

并像这样使用它

pubsub.stream.listen((event) => handleEvent(event), onDone: (){
  print('done');
});

或者,像这样重新引入撕纸片

pubsub.stream.listen(handleEvent, onDone: (){
  print('done');
});