flutter library
A Flutter library that provides RAII pattern implementation for managing the lifecycle of disposable resources.
This library offers a systematic approach to resource management in Flutter applications by automatically handling the initialization and disposal of resources. It helps prevent memory leaks and ensures proper cleanup of resources when they are no longer needed.
Key features:
- Automatic resource disposal through lifecycle management
- Fluent API for resource registration
- Debug logging support for lifecycle events
- Type-safe resource management
- Integration with Flutter's widget lifecycle
The library provides support for many Flutter resources including:
- Controllers (Animation, Text, Scroll, etc.)
- Notifiers and Listeners
- Streams
- App lifecycle
Example usage:
class MyWidgetState extends State<MyWidget>
with TickerProviderStateMixin, RaiiStateMixin {
// Resources are automatically disposed when the widget is disposed
late final controller = AnimationController(vsync: this)
.withLifecycle(this, debugLabel: 'MyAnimation');
late final textController = TextEditingController()
.withLifecycle(this, debugLabel: 'TextInput');
@override
void initLifecycle() {
super.initLifecycle();
// Register listeners with automatic cleanup
controller.addListenerWithLifecycle(
this,
() => setState(() {}),
debugLabel: 'AnimationListener',
);
}
}
The library follows these principles:
- Resources should be acquired and initialized at construction time
- Resources should be automatically released when no longer needed
- Resource cleanup should be deterministic and predictable
- The API should be simple and intuitive to use
Classes
- RaiiLifecycleAwareWithContext
- Extension of RaiiLifecycleAware that provides access to build context.
-
RaiiListenableListener<
T extends Listenable> - A lifecycle implementation that manages the lifecycle of a listener attached to a Listenable.
- RaiiTimer
- A managed Timer that integrates with the RAII lifecycle system.
- RaiiWidgetsBindingObserver
- A lifecycle implementation that manages the lifecycle of a WidgetsBindingObserver.
Mixins
-
RaiiStateMixin<
T extends StatefulWidget> - A mixin that implements RaiiLifecycleAware for StatefulWidget states.
Extensions
- AnimationControllerRaiiExt on AnimationController
- Extension for managing AnimationController lifecycle.
-
AnimationRaiiExt
on Animation<
T> - Extension that provides a more direct way to add lifecycle-managed status listeners to Animation object.
- ChangeNotifierExt on T
- Extension for managing types that extends ChangeNotifier lifecycle.
- ListenableRaiiExt on Listenable
- Extension that provides a more direct way to add lifecycle-managed listeners to any Listenable object (such as ChangeNotifier, ValueNotifier,or AnimationController).
-
StreamSubscriptionLifecycleRaiiExt
on StreamSubscription<
T> - Extension for managing StreamSubscription lifecycle.
- TickerRaiiExt on Ticker
- Extension for managing Ticker lifecycle.
- TimerRaiiExt on Timer
- Extension on Timer that adds RAII lifecycle management capabilities.
- WidgetsBindingRaiiExt on WidgetsBinding
- Extension that provides a more direct way to add lifecycle-managed observers to the WidgetsBinding instance.