Repository: rrousselGit/flutter_hooks
Pull request #244 (2021-05-19 15:06:01)

Add initialValue to useRef

TanaseHagi: any update?
codecov[bot]: # [Codecov](https://codecov.io/gh/rrousselGit/flutter_hooks/pull/244?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Remi+Rousselet) Report
> Merging [#244](https://codecov.io/gh/rrousselGit/flutter_hooks/pull/244?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Remi+Rousselet) (c76d5d9) into [master](https://codecov.io/gh/rrousselGit/flutter_hooks/commit/310cff7ff595c4d1149a824cfc5766586372a035?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Remi+Rousselet) (310cff7) will **not change** coverage.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/rrousselGit/flutter_hooks/pull/244/graphs/tree.svg?width=650&height=150&src=pr&token=9aGHUI8Y7c&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Remi+Rousselet)](https://codecov.io/gh/rrousselGit/flutter_hooks/pull/244?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Remi+Rousselet)

```diff
@@            Coverage Diff            @@
##            master      #244   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           11        11           
  Lines          614       615    +1     
=========================================
+ Hits           614       615    +1     
```


| [Impacted Files](https://codecov.io/gh/rrousselGit/flutter_hooks/pull/244?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Remi+Rousselet) | Coverage Δ | |
|---|---|---|
| [packages/flutter\_hooks/lib/src/primitives.dart](https://codecov.io/gh/rrousselGit/flutter_hooks/pull/244/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Remi+Rousselet#diff-cGFja2FnZXMvZmx1dHRlcl9ob29rcy9saWIvc3JjL3ByaW1pdGl2ZXMuZGFydA==) | `100.00% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/rrousselGit/flutter_hooks/pull/244?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Remi+Rousselet).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Remi+Rousselet)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/rrousselGit/flutter_hooks/pull/244?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Remi+Rousselet). Last update [310cff7...c76d5d9](https://codecov.io/gh/rrousselGit/flutter_hooks/pull/244?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Remi+Rousselet). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Remi+Rousselet).

rrousselGit: Thanks!

Patch:
diff --git a/packages/flutter_hooks/lib/src/primitives.dart b/packages/flutter_hooks/lib/src/primitives.dart
index 699a08b..fb66146 100644
--- a/packages/flutter_hooks/lib/src/primitives.dart
+++ b/packages/flutter_hooks/lib/src/primitives.dart
@@ -4,10 +4,15 @@ part of 'hooks.dart';
 ///
 /// It is typically created by [useRef].
 class ObjectRef<T> {
-  /// A mutable property that will be preserved accross rebuilds.
+  /// A class that stores a single value;
+  ///
+  /// It is typically created by [useRef].
+  ObjectRef(this.value);
+
+  /// A mutable property that will be preserved across rebuilds.
   ///
   /// Updating this property will not cause widgets to rebuild.
-  T? value;
+  T value;
 }
 
 /// Creates an object that contains a single mutable property.
@@ -15,8 +20,8 @@ class ObjectRef<T> {
 /// Mutating the object's property has no effect.
 /// This is useful for sharing state accross `build` calls, without causing
 /// unnecessary rebuilds.
-ObjectRef<T> useRef<T>() {
-  return useMemoized(() => ObjectRef<T>());
+ObjectRef<T> useRef<T>(T initialValue) {
+  return useMemoized(() => ObjectRef<T>(initialValue));
 }
 
 /// Cache a function accross rebuilds based on a list of keys.


Test patch:
diff --git a/packages/flutter_hooks/test/memoized_test.dart b/packages/flutter_hooks/test/memoized_test.dart
index 906c46c..da5eda4 100644
--- a/packages/flutter_hooks/test/memoized_test.dart
+++ b/packages/flutter_hooks/test/memoized_test.dart
@@ -11,30 +11,58 @@ void main() {
     reset(valueBuilder);
   });
 
-  testWidgets('useRef', (tester) async {
+  testWidgets('useRef with null initial value', (tester) async {
     late ObjectRef<int?> ref;
 
     await tester.pumpWidget(
       HookBuilder(builder: (context) {
-        ref = useRef();
+        ref = useRef<int?>(null);
         return Container();
       }),
     );
 
-    expect(ref.value, null);
+    expect(ref.value, null, reason: 'The ref value has the initial set value.');
     ref.value = 42;
 
     late ObjectRef<int?> ref2;
 
     await tester.pumpWidget(
       HookBuilder(builder: (context) {
-        ref2 = useRef();
+        ref2 = useRef<int?>(null);
         return Container();
       }),
     );
 
-    expect(ref2, ref);
-    expect(ref2.value, 42);
+    expect(ref2, ref, reason: 'The ref value remains the same after rebuild.');
+    expect(ref2.value, 42,
+        reason: 'The ref value has the last assigned value.');
+  });
+
+  testWidgets('useRef with non-null initial value', (tester) async {
+    late ObjectRef<int?> ref;
+
+    await tester.pumpWidget(
+      HookBuilder(builder: (context) {
+        ref = useRef<int>(41);
+        return Container();
+      }),
+    );
+
+    expect(ref.value, 41, reason: 'The ref value has the initial set value.');
+    ref.value = 42;
+
+    late ObjectRef<int?> ref2;
+
+    await tester.pumpWidget(
+      HookBuilder(builder: (context) {
+        ref2 = useRef<int>(43);
+        return Container();
+      }),
+    );
+
+    expect(ref2, ref, reason: 'The ref value remains the same after rebuild.');
+    expect(ref2.value, 42,
+        reason: 'The ref value has the last assigned value.');
   });
 
   testWidgets('useCallback', (tester) async {