skip to Main Content

i am using dartis 0.5.0 for my redis connection and copied the code from pubsub example folder,here it is;

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

and this my terminal message;

I/flutter (26344): connected

I/flutter (26344): SubscriptionEvent: {command=psubscribe, channel=commands, channelCount=1}

I/flutter (26344): MessageEvent<String, String>: {channel=commands, message=7, pattern=commands}

the code works but i need to get MessageEvent’ message and assign it to string variable,im probably couple lines of code far but couldnt succeeded yet.Further more, its inside of main’ body during now for testing and stuff but it’ll be in one of the stateful widget in the project so im thinking to write a function with returning to string,then i’ll be able to use it somewhere else so yeah i hope i made it clear,if you guys can help me to figure this out,i would be really appreciated

2

Answers


  1. Chosen as BEST ANSWER
    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));
    

    it was super easy :) like, i mentioned in the questions just im super new to ascyn programming, here is the answer,i hope it would be useful for someone else.


  2. Your first argument to broker.stream.listen is where each event is sent, in this case "print" which is dutifully printing out to the terminal. Change that to your own method call to handle the event:

    broker.stream.listen(callYourMethodHere, onError: print, onDone: () => exit(0));
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search