I'm building a widget that listens for UDP packets on the network and then updates the child widgets accordingly.
I'm not sure where to put the init code for the socket. It works when I put the call in the build() function but I don't think this is correct as build would be called multiple times.
I have tried creating an initState method but it never gets called.
Any pointers?
import 'dart:io';
import 'dart:convert';
import 'package:flutter/material.dart';
void connectAndListen() {
int port = 3001;
// listen forever
RawDatagramSocket.bind(InternetAddress.anyIPv4, port).then((socket) {
socket.listen((RawSocketEvent event) {
if (event == RawSocketEvent.read) {
Datagram? dg = socket.receive();
if (dg == null) return;
final recvd = String.fromCharCodes(dg.data);
/// send ack to anyone who sends ping
if (recvd == "ping") socket.send(Utf8Codec().encode("ping ack"), dg.address, port);
print("$recvd from ${dg.address.address}:${dg.port}");
}
});
});
print("udp listening on $port");
}
@override
Widget build(BuildContext context) {
connectAndListen();
return Scaffold(
appBar: AppBar(
title: const Text('Level Details'),
),
body: Column(children: [
]));
}
}