Search code examples
flutterdartmemory-leakstimer

Calling setState inside the Timer will cause memory leak in Flutter?


I'm developing a flutter application. in this application I calls setState() inside the Timer. I initiated the Timer inside the initState(). I attached my code below, this code will cause the memory leak? Thank you

import 'dart:async';
import 'package:androidtv/Utils/StringConstants.dart';
import 'package:androidtv/Utils/TimerConstants.dart';
import 'package:flutter/material.dart';
import 'package:flutter_analog_clock/flutter_analog_clock.dart';

class CustomAnalogClock extends StatefulWidget {
  final DateTime countryTime;

  const CustomAnalogClock({Key key, this.countryTime}) : super(key: key);
  @override
  _CustomAnalogClockState createState() => _CustomAnalogClockState(countryTime);
}

class _CustomAnalogClockState extends State<CustomAnalogClock> {
  DateTime countryTime;
  _CustomAnalogClockState(this.countryTime);

  Timer timer;

  @override
  void initState() {
    timer = Timer.periodic(Duration(seconds: TimerConstants.ANALOG_CLOCK_TIMER), (Timer t) => setupWorldTime());
    super.initState();
  }

  setupWorldTime() {
    setState(() {});
  }

  @override
  void dispose() {
    timer?.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return FlutterAnalogClock(
      dateTime: countryTime,
      hourHandColor: Colors.black,
      minuteHandColor: Colors.black,
      secondHandColor: Colors.red,
      numberColor: Colors.black,
      tickColor: Colors.black,
      centerPointColor: Colors.black,
      showMinuteHand: true,
      showSecondHand: true,
      showNumber: true,
      borderWidth: 0.5,
      showTicks: true,
      hourNumberScale: 1.40,
      hourNumbers: [
        StringConstants.CLOCK_NO_01,
        StringConstants.CLOCK_NO_02,
        StringConstants.CLOCK_NO_03,
        StringConstants.CLOCK_NO_04,
        StringConstants.CLOCK_NO_05,
        StringConstants.CLOCK_NO_06,
        StringConstants.CLOCK_NO_07,
        StringConstants.CLOCK_NO_08,
        StringConstants.CLOCK_NO_09,
        StringConstants.CLOCK_NO_10,
        StringConstants.CLOCK_NO_11,
        StringConstants.CLOCK_NO_12
      ],
      isLive: true,
      decoration: BoxDecoration(
          border: Border.all(width: 2.0, color: Colors.black),
          color: Colors.transparent,
          shape: BoxShape.circle),
    );
  }
}

Solution

  • I faced the same problem earlier, Your code will not face a memory leakage problem as you are cancelling the periodic timer in dispose() method.

    but your code should not run in flutter 2. also as @pskink mentioned There can be a problem inside FlutterAnalogClock

    late Timer timer;
    
      @override
      void initState() {
        timer = Timer.periodic(Duration(seconds: 1), (timer) {
          setState(() {});
        });
        super.initState();
      }
    
      @override
      void dispose() {
        timer.cancel();
        super.dispose();
      }