[英]how can i assign MessageEvents' message to string variable in dart/flutter/redis
我正在为我的 redis 连接使用dartis 0.5.0 ,并从 pubsub 示例文件夹中复制了代码,在这里;
void main() async {
runApp(MyApp());
final broker =
await redis.PubSub.connect<String, String>('redis://localhost:6379');
print('connected');
ProcessSignal.sigint.watch().listen((_) async {
await broker.disconnect();
exit(0);});
// Outputs the data received from the server.
broker.stream.listen(print,onError: print,onDone: () => exit(0));
broker.psubscribe(pattern: 'commands');
}
这是我的终端信息;
I/颤振(26344):已连接
I/flutter (26344): SubscriptionEvent: {command=psubscribe, channel=commands, channelCount=1}
I/flutter (26344): MessageEvent<String, String>: {channel=commands, message=7, pattern=commands}
代码有效,但我需要获取 MessageEvent' 消息并将其分配给字符串变量,我可能还有几行代码但还没有成功。此外,它现在在主体内部进行测试和其他东西,但它会是在项目中的一个有状态小部件中,所以我想写一个 function 并返回字符串,然后我就可以在其他地方使用它所以是的,我希望我说清楚了,如果你们能帮我解决这个问题出来,我将不胜感激
您对 broker.stream.listen 的第一个参数是发送每个事件的位置,在这种情况下,“打印”是尽职尽责地打印到终端。 将其更改为您自己的方法调用来处理事件:
broker.stream.listen(callYourMethodHere, onError: print, onDone: () => exit(0));
broker.stream.listen((event){
if(event is MessageEvent){
print(event.message);
if(event.message == '7'){
Navigator.push(
context, MaterialPageRoute(builder: (context) => Majors()));
}
}else{
print(event);
}
},onError: print,onDone: () => exit(0));
这非常简单:) 就像我在问题中提到的,我对 ascyn 编程非常陌生,这是答案,我希望它对其他人有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.