# Flutter test: A Timer is still pending even after the widget tree was disposed

**Code:** `A Timer is still pending even after the widget tree was disposed` · **Category:** Flutter / Dart · **Seen on:** Flutter

A widget test finished while a `Timer` or delayed `Future` was still scheduled.

## Meaning

Flutter tests fail if timers outlive the test. Cancel timers in `dispose()`, advance fake time with `await tester.pump(duration)` or `pumpAndSettle`, and avoid real delays. Libraries with periodic timers (animations, polling) need a mock or dispose.

## Common causes

- Timer/Future.delayed not cancelled in dispose()
- Test ended before the timer fired
- Periodic polling or retry timers
- Animation looping forever (pumpAndSettle timeout)
- Third-party plugin timers

## Quick fix

1. Cancel timers in `dispose()`
2. Advance time: `await tester.pump(const Duration(seconds: 2));`
3. Mock the timer-owning service in tests

## Detailed fix by platform

### Flutter

1. In tests, replace delays with `FakeAsync` or inject a short duration

## How to diagnose

1. **Timer** — Where created?
2. **Dispose** — Cancelled?
3. **Test** — Time advanced?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/flutter/pending-timer-after-test)
