ValueNotifier Integration
Jolt provides seamless integration with Flutter's ValueNotifier system, allowing bidirectional conversion between Jolt signals and Flutter's ValueNotifier.
JoltValueNotifier
JoltValueNotifier is a ValueNotifier implementation that wraps Jolt reactive values, providing Flutter's ValueNotifier interface. This allows you to seamlessly integrate Jolt signals with Flutter Widgets and state management systems.
Basic Usage
import 'package:jolt_flutter/jolt_flutter.dart';
final signal = Signal(42);
final notifier = JoltValueNotifier(signal);
// Use with AnimatedBuilder
AnimatedBuilder(
animation: notifier,
builder: (context, child) => Text('${notifier.value}'),
)Extension Methods
Jolt provides extension methods that let you directly get ValueNotifier from reactive values:
final counter = Signal(0);
final notifier = counter.notifier;
// Use with Flutter widgets
ValueListenableBuilder<int>(
valueListenable: notifier,
builder: (context, value, child) => Text('$value'),
)Caching Mechanism
The notifier extension method returns a cached instance. Multiple calls return the same instance, ensuring performance optimization:
final counter = Signal(0);
final notifier1 = counter.notifier;
final notifier2 = counter.notifier;
print(identical(notifier1, notifier2)); // trueBidirectional Synchronization
From ValueNotifier to Signal
Jolt provides extension methods to convert ValueNotifier to reactive signals with bidirectional synchronization:
final notifier = ValueNotifier(0);
final signal = notifier.toNotifierSignal();
// Bidirectional synchronization
notifier.value = 1; // signal.value becomes 1
signal.value = 2; // notifier.value becomes 2Use Cases
This feature is particularly useful in the following scenarios:
Integrating Existing Code: If you have existing code using
ValueNotifier, you can easily convert it to Jolt signals.Third-Party Library Integration: Some third-party libraries may use
ValueNotifier, which you can convert to Jolt signals to leverage the reactive system's advantages.Progressive Migration: When migrating from
ValueNotifierto Jolt, you can gradually convert while maintaining compatibility.
Complete Examples
Using JoltValueNotifier
import 'package:flutter/material.dart';
import 'package:jolt_flutter/jolt_flutter.dart';
final counter = Signal(0);
class CounterWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
final notifier = counter.notifier;
return ValueListenableBuilder<int>(
valueListenable: notifier,
builder: (context, value, child) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Count: $value'),
ElevatedButton(
onPressed: () => counter.value++,
child: Text('Increment'),
),
],
);
},
);
}
}Using AnimatedBuilder
final opacity = Signal(1.0);
final notifier = opacity.notifier;
AnimatedBuilder(
animation: notifier,
builder: (context, child) {
return Opacity(
opacity: notifier.value,
child: child,
);
},
child: Text('Fade in/out'),
);Bidirectional Conversion
// Create Signal from ValueNotifier
final notifier = ValueNotifier<String>('Hello');
final signal = notifier.toNotifierSignal();
// Now can use in Jolt reactive system
Effect(() {
print('Signal value: ${signal.value}');
});
// Modifying either direction synchronizes
notifier.value = 'World'; // Signal also updates
signal.value = 'Jolt'; // ValueNotifier also updatesImportant Notes
Lifecycle Management:
JoltValueNotifierautomatically manages synchronization with Jolt signals. WhenValueNotifieris disposed, synchronization automatically stops.Performance Considerations: The
notifierextension method uses a caching mechanism. Multiple calls return the same instance, avoiding multiple listeners.Collection Signals: For mutable collection signals (such as
ListSignal,MapSignal, etc.),JoltValueNotifierlistens to all changes, including internal modifications.Type Safety: All conversions maintain complete type safety with compile-time type checking.
Automatic Cleanup: When Jolt signals are disposed,
JoltValueNotifierautomatically cleans up resources without manual management.