Frank Moreno

refactoring using RxInterface.notifyChildren

@@ -45,10 +45,7 @@ class GetX<T extends DisposableInterface> extends StatefulWidget { @@ -45,10 +45,7 @@ class GetX<T extends DisposableInterface> extends StatefulWidget {
45 } 45 }
46 46
47 class GetXState<T extends DisposableInterface> extends State<GetX<T>> { 47 class GetXState<T extends DisposableInterface> extends State<GetX<T>> {
48 - GetXState() {  
49 - _observer = RxNotifier();  
50 - }  
51 - RxInterface? _observer; 48 + final _observer = RxNotifier();
52 T? controller; 49 T? controller;
53 bool? _isCreator = false; 50 bool? _isCreator = false;
54 late StreamSubscription _subs; 51 late StreamSubscription _subs;
@@ -80,7 +77,7 @@ class GetXState<T extends DisposableInterface> extends State<GetX<T>> { @@ -80,7 +77,7 @@ class GetXState<T extends DisposableInterface> extends State<GetX<T>> {
80 if (widget.global && Get.smartManagement == SmartManagement.onlyBuilder) { 77 if (widget.global && Get.smartManagement == SmartManagement.onlyBuilder) {
81 controller?.onStart(); 78 controller?.onStart();
82 } 79 }
83 - _subs = _observer!.listen((data) => setState(() {}), cancelOnError: false); 80 + _subs = _observer.listen((data) => setState(() {}), cancelOnError: false);
84 super.initState(); 81 super.initState();
85 } 82 }
86 83
@@ -107,30 +104,15 @@ class GetXState<T extends DisposableInterface> extends State<GetX<T>> { @@ -107,30 +104,15 @@ class GetXState<T extends DisposableInterface> extends State<GetX<T>> {
107 } 104 }
108 } 105 }
109 _subs.cancel(); 106 _subs.cancel();
110 - _observer!.close(); 107 + _observer.close();
111 controller = null; 108 controller = null;
112 _isCreator = null; 109 _isCreator = null;
113 super.dispose(); 110 super.dispose();
114 } 111 }
115 112
116 - Widget get notifyChildren {  
117 - final observer = RxInterface.proxy;  
118 - RxInterface.proxy = _observer;  
119 - final result = widget.builder(controller!);  
120 - if (!_observer!.canUpdate) {  
121 - throw """  
122 - [Get] the improper use of a GetX has been detected.  
123 - You should only use GetX or Obx for the specific widget that will be updated.  
124 - If you are seeing this error, you probably did not insert any observable variables into GetX/Obx  
125 - or insert them outside the scope that GetX considers suitable for an update  
126 - (example: GetX => HeavyWidget => variableObservable).  
127 - If you need to update a parent widget and a child widget, wrap each one in an Obx/GetX.  
128 - """;  
129 - }  
130 - RxInterface.proxy = observer;  
131 - return result;  
132 - }  
133 -  
134 @override 113 @override
135 - Widget build(BuildContext context) => notifyChildren; 114 + Widget build(BuildContext context) => RxInterface.notifyChildren(
  115 + _observer,
  116 + () => widget.builder(controller!),
  117 + );
136 } 118 }
@@ -20,17 +20,13 @@ abstract class ObxWidget extends StatefulWidget { @@ -20,17 +20,13 @@ abstract class ObxWidget extends StatefulWidget {
20 } 20 }
21 21
22 class _ObxState extends State<ObxWidget> { 22 class _ObxState extends State<ObxWidget> {
23 - RxInterface? _observer; 23 + final _observer = RxNotifier();
24 late StreamSubscription subs; 24 late StreamSubscription subs;
25 25
26 - _ObxState() {  
27 - _observer = RxNotifier();  
28 - }  
29 -  
30 @override 26 @override
31 void initState() { 27 void initState() {
32 - subs = _observer!.listen(_updateTree, cancelOnError: false);  
33 super.initState(); 28 super.initState();
  29 + subs = _observer.listen(_updateTree, cancelOnError: false);
34 } 30 }
35 31
36 void _updateTree(_) { 32 void _updateTree(_) {
@@ -42,30 +38,13 @@ class _ObxState extends State<ObxWidget> { @@ -42,30 +38,13 @@ class _ObxState extends State<ObxWidget> {
42 @override 38 @override
43 void dispose() { 39 void dispose() {
44 subs.cancel(); 40 subs.cancel();
45 - _observer!.close(); 41 + _observer.close();
46 super.dispose(); 42 super.dispose();
47 } 43 }
48 44
49 - Widget get notifyChilds {  
50 - final observer = RxInterface.proxy;  
51 - RxInterface.proxy = _observer;  
52 - final result = widget.build();  
53 - if (!_observer!.canUpdate) {  
54 - throw """  
55 - [Get] the improper use of a GetX has been detected.  
56 - You should only use GetX or Obx for the specific widget that will be updated.  
57 - If you are seeing this error, you probably did not insert any observable variables into GetX/Obx  
58 - or insert them outside the scope that GetX considers suitable for an update  
59 - (example: GetX => HeavyWidget => variableObservable).  
60 - If you need to update a parent widget and a child widget, wrap each one in an Obx/GetX.  
61 - """;  
62 - }  
63 - RxInterface.proxy = observer;  
64 - return result;  
65 - }  
66 -  
67 @override 45 @override
68 - Widget build(BuildContext context) => notifyChilds; 46 + Widget build(BuildContext context) =>
  47 + RxInterface.notifyChildren(_observer, widget.build);
69 } 48 }
70 49
71 /// The simplest reactive widget in GetX. 50 /// The simplest reactive widget in GetX.