Add "empty" status to rx_notifier as in many cases we need to show some widgets …
…when willing list is empty (response is OK and data fetched correctly but its inside list is null or blank)
Showing
1 changed file
with
27 additions
and
7 deletions
1 | import 'package:flutter/foundation.dart'; | 1 | import 'package:flutter/foundation.dart'; |
2 | import 'package:flutter/material.dart'; | 2 | import 'package:flutter/material.dart'; |
3 | import 'package:flutter/scheduler.dart'; | 3 | import 'package:flutter/scheduler.dart'; |
4 | + | ||
4 | import '../../../instance_manager.dart'; | 5 | import '../../../instance_manager.dart'; |
5 | import '../../get_state_manager.dart'; | 6 | import '../../get_state_manager.dart'; |
6 | import '../simple/list_notifier.dart'; | 7 | import '../simple/list_notifier.dart'; |
@@ -123,10 +124,11 @@ abstract class GetNotifier<T> extends Value<T> with GetLifeCycleBase { | @@ -123,10 +124,11 @@ abstract class GetNotifier<T> extends Value<T> with GetLifeCycleBase { | ||
123 | 124 | ||
124 | extension StateExt<T> on StateMixin<T> { | 125 | extension StateExt<T> on StateMixin<T> { |
125 | Widget obx( | 126 | Widget obx( |
126 | - NotifierBuilder<T> widget, { | ||
127 | - Widget Function(String error) onError, | ||
128 | - Widget onLoading, | ||
129 | - }) { | 127 | + NotifierBuilder<T> widget, { |
128 | + Widget Function(String error) onError, | ||
129 | + Widget onLoading, | ||
130 | + Widget onEmpty, | ||
131 | + }) { | ||
130 | assert(widget != null); | 132 | assert(widget != null); |
131 | return SimpleBuilder(builder: (_) { | 133 | return SimpleBuilder(builder: (_) { |
132 | if (status.isLoading) { | 134 | if (status.isLoading) { |
@@ -134,10 +136,13 @@ extension StateExt<T> on StateMixin<T> { | @@ -134,10 +136,13 @@ extension StateExt<T> on StateMixin<T> { | ||
134 | } else if (status.isError) { | 136 | } else if (status.isError) { |
135 | return onError != null | 137 | return onError != null |
136 | ? onError(status.errorMessage) | 138 | ? onError(status.errorMessage) |
137 | - : Center(child: Text('A error occured: ${status.errorMessage}')); | ||
138 | - } else { | ||
139 | - return widget(value); | 139 | + : Center(child: Text('A error occurred: ${status.errorMessage}')); |
140 | + } else if (status.isEmpty) { | ||
141 | + return onEmpty != null | ||
142 | + ? onEmpty | ||
143 | + : SizedBox.shrink(); // Also can be widget(null); but is risky | ||
140 | } | 144 | } |
145 | + return widget(value); | ||
141 | }); | 146 | }); |
142 | } | 147 | } |
143 | } | 148 | } |
@@ -146,8 +151,11 @@ class RxStatus { | @@ -146,8 +151,11 @@ class RxStatus { | ||
146 | final bool isLoading; | 151 | final bool isLoading; |
147 | final bool isError; | 152 | final bool isError; |
148 | final bool isSuccess; | 153 | final bool isSuccess; |
154 | + final bool isEmpty; | ||
149 | final String errorMessage; | 155 | final String errorMessage; |
156 | + | ||
150 | RxStatus._({ | 157 | RxStatus._({ |
158 | + this.isEmpty, | ||
151 | this.isLoading, | 159 | this.isLoading, |
152 | this.isError, | 160 | this.isError, |
153 | this.isSuccess, | 161 | this.isSuccess, |
@@ -159,6 +167,7 @@ class RxStatus { | @@ -159,6 +167,7 @@ class RxStatus { | ||
159 | isLoading: true, | 167 | isLoading: true, |
160 | isError: false, | 168 | isError: false, |
161 | isSuccess: false, | 169 | isSuccess: false, |
170 | + isEmpty: false, | ||
162 | ); | 171 | ); |
163 | } | 172 | } |
164 | 173 | ||
@@ -167,6 +176,7 @@ class RxStatus { | @@ -167,6 +176,7 @@ class RxStatus { | ||
167 | isLoading: false, | 176 | isLoading: false, |
168 | isError: false, | 177 | isError: false, |
169 | isSuccess: true, | 178 | isSuccess: true, |
179 | + isEmpty: false, | ||
170 | ); | 180 | ); |
171 | } | 181 | } |
172 | 182 | ||
@@ -175,9 +185,19 @@ class RxStatus { | @@ -175,9 +185,19 @@ class RxStatus { | ||
175 | isLoading: false, | 185 | isLoading: false, |
176 | isError: true, | 186 | isError: true, |
177 | isSuccess: false, | 187 | isSuccess: false, |
188 | + isEmpty: false, | ||
178 | errorMessage: message, | 189 | errorMessage: message, |
179 | ); | 190 | ); |
180 | } | 191 | } |
192 | + | ||
193 | + factory RxStatus.empty() { | ||
194 | + return RxStatus._( | ||
195 | + isLoading: false, | ||
196 | + isError: false, | ||
197 | + isSuccess: false, | ||
198 | + isEmpty: true, | ||
199 | + ); | ||
200 | + } | ||
181 | } | 201 | } |
182 | 202 | ||
183 | typedef NotifierBuilder<T> = Widget Function(T state); | 203 | typedef NotifierBuilder<T> = Widget Function(T state); |
-
Please register or login to post a comment