PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, October 22, 2022

[FIXED] Where to put widget init/deinit code in Flutter

 October 22, 2022     dart, flutter, sockets     No comments   

Issue

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


        ]));
  }
}

Solution

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);
}


Answered By - CopsOnRoad
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing