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
- RaiiDisposeable
- A lifecycle implementation that wraps a dispose callback and manages its lifecycle.
-
RaiiListenableListener<
T extends Listenable> - A lifecycle implementation that manages the lifecycle of a listener attached to a Listenable.
- 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.
- ClipboardStatusNotifierRaiiExt on ClipboardStatusNotifier
- Extension for managing ClipboardStatusNotifier lifecycle.
- DraggableScrollableControllerRaiiExt on DraggableScrollableController
- Extension for managing DraggableScrollableController lifecycle.
- FixedExtentScrollControllerRaiiExt on FixedExtentScrollController
- Extension for managing FixedExtentScrollController lifecycle.
- FocusManagerRaiiExt on FocusManager
- Extension for managing FocusManager lifecycle.
- FocusNodeRaiiExt on FocusNode
- Extension for managing FocusNode lifecycle.
- FocusScopeNodeRaiiExt on FocusScopeNode
- Extension for managing FocusScopeNode lifecycle.
- KeepAliveHandleRaiiExt on KeepAliveHandle
- Extension for managing KeepAliveHandle 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).
- MouseTrackerRaiiExt on MouseTracker
- Extension for managing MouseTracker lifecycle.
- PageControllerRaiiExt on PageController
- Extension for managing PageController lifecycle.
- PlatformRouteInformationProviderRaiiExt on PlatformRouteInformationProvider
- Extension for managing PlatformRouteInformationProvider lifecycle.
- RestorableBoolNRaiiExt on RestorableBoolN
- Extension for managing nullable RestorableBoolN lifecycle.
- RestorableBoolRaiiExt on RestorableBool
- Extension for managing RestorableBool lifecycle.
- RestorableDateTimeNRaiiExt on RestorableDateTimeN
- Extension for managing nullable RestorableDateTimeN lifecycle.
- RestorableDateTimeRaiiExt on RestorableDateTime
- Extension for managing RestorableDateTime lifecycle.
- RestorableDoubleNRaiiExt on RestorableDoubleN
- Extension for managing nullable RestorableDoubleN lifecycle.
- RestorableDoubleRaiiExt on RestorableDouble
- Extension for managing RestorableDouble lifecycle.
-
RestorableEnumNRaiiRaiiExt
on RestorableEnumN<
T> - Extension for managing nullable RestorableEnumN lifecycle.
-
RestorableEnumRaiiExt
on RestorableEnum<
T> - Extension for managing RestorableEnum lifecycle.
- RestorableIntNRaiiExt on RestorableIntN
- Extension for managing nullable RestorableIntN lifecycle.
- RestorableIntRaiiExt on RestorableInt
- Extension for managing RestorableInt lifecycle.
-
RestorableNumNRaiiExt
on RestorableNumN<
T> - Extension for managing nullable RestorableNumN lifecycle.
-
RestorableNumRaiiExt
on RestorableNum<
T> - Extension for managing RestorableNum lifecycle.
-
RestorableRouteFutureRaiiExt
on RestorableRouteFuture<
T> - Extension for managing RestorableRouteFuture lifecycle.
- RestorableStringNRaiiExt on RestorableStringN
- Extension for managing nullable RestorableStringN lifecycle.
- RestorableStringRaiiExt on RestorableString
- Extension for managing RestorableString lifecycle.
- RestorableTextEditingControllerRaiiExt on RestorableTextEditingController
- Extension for managing RestorableTextEditingController lifecycle.
- RestorationManagerRaiiExt on RestorationManager
- Extension for managing RestorationManager lifecycle.
- ScrollbarPainterRaiiExt on ScrollbarPainter
- Extension for managing ScrollbarPainter lifecycle.
- ScrollControllerRaiiExt on ScrollController
- Extension for managing ScrollController lifecycle.
- ScrollPositionRaiiExt on ScrollPosition
- Extension for managing ScrollPosition lifecycle.
- ScrollPositionWithSingleContextRaiiExt on ScrollPositionWithSingleContext
- Extension for managing ScrollPositionWithSingleContext lifecycle.
- SemanticsOwnerRaiiExt on SemanticsOwner
- Extension for managing SemanticsOwner lifecycle.
- ShortcutManagerRaiiExt on ShortcutManager
- Extension for managing ShortcutManager lifecycle.
- ShortcutRegistryRaiiExt on ShortcutRegistry
- Extension for managing ShortcutRegistry lifecycle.
- SliverOverlapAbsorberHandleRaiiExt on SliverOverlapAbsorberHandle
- Extension for managing SliverOverlapAbsorberHandle lifecycle.
- SnapshotControllerRaiiExt on SnapshotController
- Extension for managing SnapshotController lifecycle.
-
StreamSubscriptionLifecycleRaiiExt
on StreamSubscription<
T> - Extension for managing StreamSubscription lifecycle.
- TextEditingControllerRaiiExt on TextEditingController
- Extension for managing TextEditingController lifecycle.
- TickerRaiiExt on Ticker
- Extension for managing Ticker lifecycle.
- TrackingScrollControllerRaiiExt on TrackingScrollController
- Extension for managing TrackingScrollController lifecycle.
- TransformationControllerRaiiExt on TransformationController
- Extension for managing TransformationController lifecycle.
- UndoHistoryControllerRaiiExt on UndoHistoryController
- Extension for managing UndoHistoryController lifecycle.
-
ValueNotifierRaiiExt
on ValueNotifier<
T> - Extension for managing ValueNotifier lifecycle.
- ViewportOffsetRaiiExt on ViewportOffset
- Extension for managing ViewportOffset lifecycle.
- WidgetsBindingRaiiExt on WidgetsBinding
- Extension that provides a more direct way to add lifecycle-managed observers to the WidgetsBinding instance.