Home > Enterprise >  return a value from a socket in flutter
return a value from a socket in flutter

Time:09-17

I wrote a void function that allows me to have the data of a socket of a nodejs server like this one

  Future<void> getCoordinate() async{
    //call socket
    GeocoordinateModel geocoordinateObject;
    Map<String,dynamic> geocoordinateMap;
   IO.Socket socket = IO.io(url,OptionBuilder().setTransports(['websocket']).build());
   socket.on("connect", (data) => {
     socket.on('geocoordinateData',(message)=>{
       if(message){
         geocoordinateMap = Map<String,dynamic>.from(message['payload']),
         geocoordinateObject =  GeocoordinateModel.fromMap(geocoordinateMap),
         print(geocoordinateObject),
       }
  }),
  });

  //return geocoordinateObject;
  }

now, now I would like to return the varibla geocoordinateObject of the function i try this code, but it it didn't work

import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:real_chat/data/model/geocoordinate_model.dart';
import 'package:socket_io_client/socket_io_client.dart' as IO;
import 'package:socket_io_client/socket_io_client.dart';
class GeocoordinateApi {
  static const String url = 'https://xxx.herokuapp.com/';
  Future<GeocoordinateModel?> getCoordinate() async{
    //call socket
    GeocoordinateModel? geocoordinateObject;
    Map<String,dynamic> geocoordinateMap;
   IO.Socket socket = IO.io(url,OptionBuilder().setTransports(['websocket']).build());
   socket.on("connect", (data) => {
     socket.on('geocoordinateData',(message)=>{
       if(message){
         geocoordinateMap = Map<String,dynamic>.from(message['payload']),
         geocoordinateObject =  GeocoordinateModel.fromMap(geocoordinateMap),
         print(geocoordinateObject),
       }
  }),
  });

  return geocoordinateObject;
  }
}

but it returns null

CodePudding user response:

Use a completer to create your own future.

Steps

  1. Instantiate a completer
final completer = Completer<GeocoordinateModel?>();
  1. Complete the completer
completer.complete(geocoordinateObject);
  1. Return the future
return completer.future;

Code

Future<GeocoordinateModel?> getCoordinate() async {
  GeocoordinateModel? geocoordinateObject;
  Map<String, dynamic> geocoordinateMap;
  IO.Socket socket =
      IO.io(url, OptionBuilder().setTransports(['websocket']).build());
  // [NEW CODE]: Add a completer
  final completer = Completer<GeocoordinateModel?>();
  // You don't have to listen for 'connect'.
  socket.on(
    'geocoordinateData',
    (message) => {
      // if(message) doesn't check for null. It should be if(message != null).
      // If this is null safe code, you don't have to check for null.
      geocoordinateMap = Map<String, dynamic>.from(message['payload']),
      geocoordinateObject = GeocoordinateModel.fromMap(geocoordinateMap),
      // [NEW CODE]: send your object to the completer.
      completer.complete(geocoordinateObject);
    },
  );

  // return the future.
  return completer.future;
}
  • Related