The lightest signal library for Dart, ported from stackblitz/alien-signals.
Tip
Alien Signals is the fastest signal library currently, as shown by experimental results from 👉 dart-reactivity-benchmark.
To install Alien Signals, add the following to your pubspec.yaml
:
dependencies:
alien_signals: latest
Alternatively, you can run the following command:
dart pub add alien_signals
import 'package:alien_signals/alien_signals.dart';
void main() {
// Create a signal
final count = signal(0);
// Create a computed value
final doubled = computed((_) => count.get() * 2);
// Create an effect
effect(() {
print('Count: ${count.get()}, Doubled: ${doubled.get()}');
});
// Update the signal
count.set(1); // Prints: Count: 1, Doubled: 2
}
Signals are reactive values that notify subscribers when they change:
final name = signal('Alice');
print(name.get()); // Get value using `get` method.
name.set('Bob'); // Set value using `set` method.
Computed values automatically derive from other reactive values:
final firstName = signal('John');
final lastName = signal('Doe');
final fullName = computed((_) => '${firstName.get()} ${lastName.get()}');
effect(() => print(fullName.get())); // Prints: John Doe
lastName.set('Smith'); // Prints: John Smith
Effects run automatically when their dependencies change:
final user = signal('guest');
final e = effect(() {
print('Current user: ${user.get()}');
});
// Cleanup when done
e.stop();
Group and manage related effects:
final scope = effectScope();
scope.run(() {
// Effects created here are grouped
effect(() => print('Effect 1'));
effect(() => print('Effect 2'));
});
// Clean up all effects in scope
scope.stop();
See the API documentation for detailed information about all available APIs.
This project is licensed under the MIT License - see the LICENSE file for details.
This is a Dart port of the excellent stackblitz/alien-signals library.