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.periodickeeps running in backgroundStreamSubscriptioncontinues 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
Futureexecution - β 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