'Where to put widget init/deinit code in Flutter

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?

Thanks

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: [


        ]));
  }
}


Solution 1:[1]

I'm not aware of what's going on in your code. But if it's working inside build (which can definitely get called multiple times), you can use it in initState also but inside a microtask queue.

@override
void initState() {
  super.initState();
  
  // Call your method from microtask queue. This will only get called once. 
  Future.microtask(() {
    connectAndListen();
  });

  // Or better:
  Future.microtask(connectAndListen);
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 CopsOnRoad