Beside ChangeNotifier
Signals stand beside ChangeNotifier, not instead of it. @ChangeNotifierComponent and @NotifyListeners of the Flutter starter stay as they are. Signals are a second way, for a bean whose fields change apart from each other.
Why
A state bean of the Flutter starter notifies after every method: one notification for the whole bean. That is right for a bean that is one thing, such as a counter or a selection. A bean with several fields that change apart from each other (a basket's items, its coupon, a catalog's genre and its books) rebuilds every listener for every change. Signals tell by field.
Example
The same basket, both ways:
// One notification for the bean, after every method.
@ChangeNotifierComponent()
class Basket {
final _items = <Book>[];
int get count => _items.length;
void add(Book book) => _items.add(book);
}
// By field: a reader of `coupon` is not told when `items` changes.
@Component()
class Basket {
@Signal()
List<Book> items = [];
@Signal()
String? coupon;
void add(Book book) => items.add(book);
}
Which for what
| State beans | Signals | Bloc | |
|---|---|---|---|
| Package | spring_boot_starter_flutter |
spring_boot_starter_flutter_signals |
flutter_bloc, the app's own dependency |
| On the bean | @ChangeNotifierComponent, or @NotifyListeners on a ChangeNotifier |
@Signal, @Computed, @Effect on members of a component |
The bloc is a component |
| What is told | The bean's listeners, after a method | The readers of a field, when it is assigned | The listeners of the bloc's stream, for a new state |
| A Spring widget is built again | When its build read the bean |
For the fields its build read |
Not by itself: BlocBuilder is used |
| Values made from others | Getters, computed on every read | @Computed, kept until what it read changes |
States the bloc emits |
A Future or a Stream with its state |
Written by hand | @Computed, with state, refresh(), reload() |
Written as states |
The three live in one app: each is a bean, injected and scoped the same way.
- State beans:
@ChangeNotifierComponentand@NotifyListeners. - Bloc and Cubit: the third way, with no starter of its own.