Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 81c7833

Browse filesBrowse files
authored
Make useCallback hook's keys optional (#369)
1 parent 2a6b45c commit 81c7833
Copy full SHA for 81c7833

File tree

Expand file treeCollapse file tree

4 files changed

+76
-39
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+76
-39
lines changed

‎packages/flutter_hooks/CHANGELOG.md

Copy file name to clipboardExpand all lines: packages/flutter_hooks/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## Unreleased minor
22

33
- Added `useSearchController` (thanks to @snapsl)
4+
- Keys on `useCallback` are now optional, to match `useMemoized` (thanks to @jezsung)
45

56
## 0.18.6
67

‎packages/flutter_hooks/lib/src/primitives.dart

Copy file name to clipboardExpand all lines: packages/flutter_hooks/lib/src/primitives.dart
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ ObjectRef<T> useRef<T>(T initialValue) {
4242
/// }, [key]);
4343
/// ```
4444
T useCallback<T extends Function>(
45-
T callback,
46-
List<Object?> keys,
47-
) {
45+
T callback, [
46+
List<Object?> keys = const <Object>[],
47+
]) {
4848
return useMemoized(() => callback, keys);
4949
}
5050

‎packages/flutter_hooks/test/memoized_test.dart

Copy file name to clipboardExpand all lines: packages/flutter_hooks/test/memoized_test.dart
-36Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -65,42 +65,6 @@ void main() {
6565
reason: 'The ref value has the last assigned value.');
6666
});
6767

68-
testWidgets('useCallback', (tester) async {
69-
late int Function() fn;
70-
71-
await tester.pumpWidget(
72-
HookBuilder(builder: (context) {
73-
fn = useCallback<int Function()>(() => 42, []);
74-
return Container();
75-
}),
76-
);
77-
78-
expect(fn(), 42);
79-
80-
late int Function() fn2;
81-
82-
await tester.pumpWidget(
83-
HookBuilder(builder: (context) {
84-
fn2 = useCallback<int Function()>(() => 42, []);
85-
return Container();
86-
}),
87-
);
88-
89-
expect(fn2, fn);
90-
91-
late int Function() fn3;
92-
93-
await tester.pumpWidget(
94-
HookBuilder(builder: (context) {
95-
fn3 = useCallback<int Function()>(() => 21, [42]);
96-
return Container();
97-
}),
98-
);
99-
100-
expect(fn3, isNot(fn));
101-
expect(fn3(), 21);
102-
});
103-
10468
testWidgets('memoized without parameter calls valueBuilder once',
10569
(tester) async {
10670
late int result;
+72Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import 'package:flutter/widgets.dart';
2+
import 'package:flutter_hooks/flutter_hooks.dart';
3+
4+
import 'mock.dart';
5+
6+
void main() {
7+
testWidgets('useCallback', (tester) async {
8+
late int Function() fn;
9+
10+
await tester.pumpWidget(
11+
HookBuilder(builder: (context) {
12+
fn = useCallback<int Function()>(() => 42, []);
13+
return Container();
14+
}),
15+
);
16+
17+
expect(fn(), 42);
18+
19+
late int Function() fn2;
20+
21+
await tester.pumpWidget(
22+
HookBuilder(builder: (context) {
23+
fn2 = useCallback<int Function()>(() => 42, []);
24+
return Container();
25+
}),
26+
);
27+
28+
expect(fn2, fn);
29+
30+
late int Function() fn3;
31+
32+
await tester.pumpWidget(
33+
HookBuilder(builder: (context) {
34+
fn3 = useCallback<int Function()>(() => 21, [42]);
35+
return Container();
36+
}),
37+
);
38+
39+
expect(fn3, isNot(fn));
40+
expect(fn3(), 21);
41+
});
42+
43+
testWidgets(
44+
'should return same function when keys are not specified',
45+
(tester) async {
46+
late Function fn1;
47+
late Function fn2;
48+
49+
await tester.pumpWidget(
50+
HookBuilder(
51+
key: const Key('hook_builder'),
52+
builder: (context) {
53+
fn1 = useCallback(() {});
54+
return Container();
55+
},
56+
),
57+
);
58+
59+
await tester.pumpWidget(
60+
HookBuilder(
61+
key: const Key('hook_builder'),
62+
builder: (context) {
63+
fn2 = useCallback(() {});
64+
return Container();
65+
},
66+
),
67+
);
68+
69+
expect(fn1, same(fn2));
70+
},
71+
);
72+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.