New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[0.6.0-dev] useProvider
triggers pointless rebuild when the value type is collection
#69
Comments
This line seems to cause the problem 👀 |
This was removed voluntarily Collection comparison are quite expensive, so it is usually inefficient |
Is there any way to compare collection like previously? I sometimes prefer this kind of optimization. class _ListView extends HookWidget {
const _ListView({ Key key }): super(key: key);
@override
Widget build(BuildContext context) {
final ids = useProvider(someProvider.state.select((s) => s.keys.toList()));
return ListView.builder(
itemCount: ids.length,
itemBuilder: (context, index) => _Tile(id: ids[index]),
);
}
} But I know this kind of optimization is not much needed, so it is okay if collection comparison is impossible. |
There's a different solution: final someLengthProvider = Provider((ref) {
return ref.watch(someProvider).keys.length;
});
return ListView.builder(
itemCount: useProvider(someLengthProvider),
); |
Thanks, but the detection of |
You could have : itemBuilder: (context, index) {
return HookBuilder(builder: (context) {
final id = useProvider(someProvider.select((s) => s.keys.elementAt(index))
return TodoItem(id);
});
}) |
Thanks, it suppresses the pointless rebuild and works well 👍 I think this change should be documented(although you may already be planning on it). The problem was fixed, so I'm closing the issue, thanks again! |
Such code is how Todo and Marvel deal with lists, with using ScopedProvider on the top of that to make the list item constant |
Thanks, that's much better 👍 |
Describe the bug
useProvider
triggers pointless rebuild when the value is collection type.To Reproduce
At
0.6.0-dev+2
, executed this.build
log will be printed each timePRESS ME
button is pressed.If
[42]
(List<int>
) is changed to42
(int
), the problem doesn't occurs, so collection comparison seems broken.Expected behavior
build
should be printed at first time when the UI appeared, and shouldn't be printed whenPRESS ME
button pressed.At
0.5.1
, it works well without any problems.The text was updated successfully, but these errors were encountered: