Search code examples
flutterdartmoduletimer

Flutter I have two periodic Timer only once works


I would like to create a separate periodic timer for each statefull module that repeatedly calls certain functions.

First this stateful Widget here it works the Timer:

 class Wetteddaten extends StatefulWidget{
  const Wetteddaten({Key? key}) : super(key: key);
  @override
  WetterdatenContent createState() => WetterdatenContent();
}

Extends State:

class WetterdatenContent extends State {
   WetterdatenContent() {
/*
  var periodicTimer = Timer.periodic(
    const Duration(seconds: 5),
        (timer) {

      // Update user about remaining time
      _getLocation();
    },
  );*/
}

This ist in Comments but _getLocation() runs

But in this Modul periodic Timer dosent work:

class getPositionAdress extends StatefulWidget{
  const getPositionAdress({Key? key}) : super(key: key);

  @override
  AdressCalc createState() => AdressCalc();
}

In Adress Class

class AdressCalc extends State {
  @override
  void initState() {
    Timer.periodic(const Duration(seconds: 1), (Timer timer) {

      _updateState();
    });
    super.initState();
  }
  AdressCalc() {
    var counter = 3;

}

It makes no difference whether the timer is in initState or AdressCalc().

I have no idea what could be causing this. Also updateState is executed after and before the timer but not inside

My project idea is to create a weather app with three separate stateful modules, each of which has its own periodic timer that controls the module and monitors whether something has changed in the variable values. The Main Modul is a Sateless Widget


Solution

  • I found the problem. For one, the main widget was stateless must be stateful.

    On the other hand I have included a ValueNotifier

    Which change the value of Text field.