Showing
1 changed file
with
7 additions
and
12 deletions
| @@ -5,7 +5,7 @@ import 'list_notifier.dart'; | @@ -5,7 +5,7 @@ import 'list_notifier.dart'; | ||
| 5 | 5 | ||
| 6 | typedef ValueBuilderUpdateCallback<T> = void Function(T snapshot); | 6 | typedef ValueBuilderUpdateCallback<T> = void Function(T snapshot); | 
| 7 | typedef ValueBuilderBuilder<T> = Widget Function( | 7 | typedef ValueBuilderBuilder<T> = Widget Function( | 
| 8 | - T? snapshot, ValueBuilderUpdateCallback<T> updater); | 8 | + T snapshot, ValueBuilderUpdateCallback<T> updater); | 
| 9 | 9 | ||
| 10 | /// Manages a local state like ObxValue, but uses a callback instead of | 10 | /// Manages a local state like ObxValue, but uses a callback instead of | 
| 11 | /// a Rx value. | 11 | /// a Rx value. | 
| @@ -23,36 +23,32 @@ typedef ValueBuilderBuilder<T> = Widget Function( | @@ -23,36 +23,32 @@ typedef ValueBuilderBuilder<T> = Widget Function( | ||
| 23 | /// ), | 23 | /// ), | 
| 24 | /// ``` | 24 | /// ``` | 
| 25 | class ValueBuilder<T> extends StatefulWidget { | 25 | class ValueBuilder<T> extends StatefulWidget { | 
| 26 | - final T? initialValue; | 26 | + final T initialValue; | 
| 27 | final ValueBuilderBuilder<T> builder; | 27 | final ValueBuilderBuilder<T> builder; | 
| 28 | final void Function()? onDispose; | 28 | final void Function()? onDispose; | 
| 29 | final void Function(T)? onUpdate; | 29 | final void Function(T)? onUpdate; | 
| 30 | 30 | ||
| 31 | const ValueBuilder({ | 31 | const ValueBuilder({ | 
| 32 | Key? key, | 32 | Key? key, | 
| 33 | - this.initialValue, | 33 | + required this.initialValue, | 
| 34 | this.onDispose, | 34 | this.onDispose, | 
| 35 | this.onUpdate, | 35 | this.onUpdate, | 
| 36 | required this.builder, | 36 | required this.builder, | 
| 37 | }) : super(key: key); | 37 | }) : super(key: key); | 
| 38 | 38 | ||
| 39 | @override | 39 | @override | 
| 40 | - _ValueBuilderState<T> createState() => _ValueBuilderState<T>(); | 40 | + _ValueBuilderState<T> createState() => _ValueBuilderState<T>(initialValue); | 
| 41 | } | 41 | } | 
| 42 | 42 | ||
| 43 | class _ValueBuilderState<T> extends State<ValueBuilder<T?>> { | 43 | class _ValueBuilderState<T> extends State<ValueBuilder<T?>> { | 
| 44 | - T? value; | 44 | + T value; | 
| 45 | + _ValueBuilderState(this.value); | ||
| 45 | 46 | ||
| 46 | - @override | ||
| 47 | - void initState() { | ||
| 48 | - super.initState(); | ||
| 49 | - value = widget.initialValue; | ||
| 50 | - } | ||
| 51 | 47 | ||
| 52 | @override | 48 | @override | 
| 53 | Widget build(BuildContext context) => widget.builder(value, updater); | 49 | Widget build(BuildContext context) => widget.builder(value, updater); | 
| 54 | 50 | ||
| 55 | - void updater(T? newValue) { | 51 | + void updater(T newValue) { | 
| 56 | if (widget.onUpdate != null) { | 52 | if (widget.onUpdate != null) { | 
| 57 | widget.onUpdate!(newValue); | 53 | widget.onUpdate!(newValue); | 
| 58 | } | 54 | } | 
| @@ -70,7 +66,6 @@ class _ValueBuilderState<T> extends State<ValueBuilder<T?>> { | @@ -70,7 +66,6 @@ class _ValueBuilderState<T> extends State<ValueBuilder<T?>> { | ||
| 70 | } else if (value is StreamController) { | 66 | } else if (value is StreamController) { | 
| 71 | (value as StreamController?)?.close(); | 67 | (value as StreamController?)?.close(); | 
| 72 | } | 68 | } | 
| 73 | - value = null; | ||
| 74 | } | 69 | } | 
| 75 | } | 70 | } | 
| 76 | 71 | 
- 
Please register or login to post a comment