Jessica
Jessica

Reputation: 9830

Error of: A value of type 'Future' can't be assigned to a variable of type 'SocketIO'

I'm trying to create a socket io app, and I'm using adhara_socket_io. I added the code from the example on the pub.dev site, and I'm gettin an error. Here's the code:

SocketIOManager manager = SocketIOManager();
SocketIO socket = manager.createInstance('http://192.168.1.2:7000/');  // I get the error here
socket.onConnect((data){
  print("connected...");
  print(data);
  socket.emit("message", ["Hello world!"]);
});
socket.connect();

I get two errors. First one:

A value of type 'Future' can't be assigned to a variable of type 'SocketIO'. Try changing the type of the variable, or casting the right-hand type to 'SocketIO'

And the second one:

The argument type 'String' can't be assigned to the parameter type 'SocketOptions'

What am I doing wrong, and how can I fix it?

Upvotes: 3

Views: 5666

Answers (2)

Younes Keraressi
Younes Keraressi

Reputation: 21

   //i think like Javascript for using await just wrap your function with async keyword.
   fn() async{
       SocketIOManager manager = SocketIOManager();
       SocketIO socket = await manager.createInstance('http://192.168.1.2:7000/');  
       socket.onConnect((data){
         print("connected...");
         print(data);
         socket.emit("message", ["Hello world!"]);
       });
       socket.connect();
    }

Upvotes: 0

Rodrigo Bastos
Rodrigo Bastos

Reputation: 2448

My bet is that this guy manager.createInstance returns a future so you should place await right in front of it to wait for the future to be resolved. Something like this:

SocketIO socket = await manager.createInstance('http://192.168.1.2:7000/');  

If this code is inside of a function you should marks this function as async

Upvotes: 1

Related Questions