I thought Riverpod will only trigger rebuild if the state value is different but turn out it rebuild every time when state is set although the value is the same. Is that true?
The case is as below
@Freezed(genericArgumentFactories: true)
class Model with _$Model {
const factory Model({required int id}) = _Model;
}
class Manager {
static StateProvider<Model> modelProvider =
StateProvider<Model>((ref) => Model(id: 1));
Manager() {
Stream.periodic(Duration(seconds: 1)).take(1000).listen((event) {
ref.read(modelProvider.notifier).update((state) {
var cloneState = state.copyWith();
print("${state == cloneState}"); //This print true
return cloneState;
});
});
}
}
class TestWidget extends ConsumerWidget {
const TestWidget();
@override
Widget build(BuildContext context, WidgetRef ref) {
var model = ref.watch(Manager.modelProvider);
print("model change......................"); //print every second
return Text(model.id.toString());
}
}
It showed that the TestWidget was rebuilt every seconds but I thought it shouldn’t as the state is the same although I set it again.
Am I missing something? Thanks.
2
Answers
It’s all about using
identical(old, current)
under the hood to compare states.identical
presents for itself the following:Here is a complete copy-run example:
I modified the example a little, but kept the essence the same. Only by applying `const’ can we achieve the absence of rebuilds.
Riverpod by default doesn’t rely on
==
butidentical
to filter updates.The reasoning is that
==
can be quite inefficient if your model becomes large.But this causes the behavior you described: If two objects have the same content but use a different instance, listeners will be notified.
It’s not considered a problem though, as there is no value in doing: