Return Datagrams from RawDatagramSocket (Flutter)

Viewed 609

In this link described how print all Datagrams when sending a broadcast UDP packet. But how to receive a list of them? I'm try this

List<Datagram> list = [];
...
socket.listen(event) {
list.add(socket.receive());
}
...
return list;

but this is not works.

socket.close();
await socket.toList();

save a list of events with just one event of closing them. Is there any way to save a list of datagrams?

1 Answers

Just need to wait

List<dynamic> result = [];
InternetAddress destination = InternetAddress("255.255.255.255");
    RawDatagramSocket udp =
        await RawDatagramSocket.bind(InternetAddress.anyIPv4, port);
    List<int> message = utf8.encode('test');
    udp.broadcastEnabled = true;
    udp.listen((e) {
      Datagram dg = udp.receive();
      if (dg != null) {
        result.add(dg.data);
      {
    });
udp.send(message, destination, port);
await Future.delayed(Duration(seconds: 3));
udp.close();
return result;
Related