flutter_safe_async

πŸ›‘οΈ flutter_safe_async is a lightweight Flutter package that eliminates the most common runtime crash:

❌ setState() called after dispose()

It provides lifecycle-aware async handling for Future, Timer, and Stream operations in Flutter widgets β€” with zero boilerplate.


πŸ”₯ The Problem (Very Common in Flutter)

In Flutter, asynchronous operations often outlive the widget that started them.

Typical scenarios:

  • API call finishes after user leaves the screen
  • Timer.periodic keeps running in background
  • StreamSubscription continues emitting values

This results in:

  • ❌ App crashes
  • ❌ Memory leaks
  • ❌ Unstable UI behavior

βœ… The Solution

flutter_safe_async binds async operations to the widget lifecycle.

It ensures:

  • UI updates only happen while the widget is alive
  • Timers and streams are cleaned up automatically
  • Async errors don’t trigger invalid callbacks

✨ Features

  • βœ… Safe replacement for setState
  • βœ… Automatically cancels Timer & Timer.periodic
  • βœ… Automatically cancels StreamSubscription
  • βœ… Lifecycle-safe Future execution
  • βœ… Built-in retry support for async tasks
  • βœ… No dependency on state management libraries
  • βœ… Works with any architecture
  • βœ… Production-ready & beginner-friendly

πŸ“¦ Installation

Add this to your pubspec.yaml:

dependencies:
  flutter_safe_async: ^1.0.1

Libraries

flutter_safe_async