You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/leancode_cubit_utils/README.md
+62-5Lines changed: 62 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -96,15 +96,51 @@ The cubit itself handles the things like:
96
96
97
97
If you call `refresh()` on `ArgsRequestCubit` it will perform a request with the last used arguments. They are also available under `lastRequestArgs` field.
98
98
99
+
### `RequestState`
100
+
101
+
`RequestState` represents the state of a request and can be one of the following:
102
+
-`RequestInitialState` - the request has not been started yet,
103
+
-`RequestLoadingState` - the request is currently being performed,
104
+
-`RequestSuccessState<TOut>` - the request completed successfully with data,
105
+
-`RequestErrorState<TError>` - the request failed with an error,
106
+
-`RequestRefreshingState<TOut>` - the request is being refreshed while previous data is still available,
107
+
-`RequestEmptyState<TOut>` - the request completed successfully but returned empty data.
108
+
109
+
#### `map` method
110
+
111
+
`RequestState` provides a `map` method that allows you to transform the state into a value of any type. This is useful when you need to derive a value based on the current state without building a widget. The method accepts the following parameters:
112
+
113
+
-`T Function()? initial` - called when the request is in its initial state. If not provided, falls back to `loading`,
114
+
-`T Function() loading` - called when the request is loading (required),
115
+
-`T Function(TOut? data) success` - called when the request completed successfully (required),
116
+
-`T Function(TError? err, Object? exception, StackTrace? st) error` - called when the request failed (required),
117
+
-`T Function(TOut data)? refreshing` - called when the request is refreshing with previous data. If not provided, falls back to `success`,
118
+
-`T Function(TOut? data)? empty` - called when the request completed successfully but returned empty data. If not provided, falls back to `success`.
`RequestCubitBuilder` is a widget that builds a widget based on the current state of `BaseRequestCubit`. It takes a numerous builders for each state:
102
-
-`WidgetBuilder? onInitial` - use it to show a widget before invoking the request for the first time,
135
+
`RequestCubitBuilder` is a widget that builds a widget based on the current state of `BaseRequestCubit`. It takes numerous builders for each state:
136
+
-`WidgetBuilder? onInitial` - use it to show a widget before invoking the request for the first time.
103
137
-`WidgetBuilder? onLoading` - use it to show a loader widget while the request is being performed,
104
138
-`WidgetBuilder? onError` - use it to show error widget when processing the request fails,
105
-
-`RequestWidgetBuilder<TOut> onSuccess` - use it to build a page when the data is successfully loaded.
139
+
-`RequestWidgetBuilder<TOut> onSuccess` - use it to build a page when the data is successfully loaded (required),
140
+
-`WidgetBuilder? onEmpty` - use it to show a widget when the request returns empty data. If not provided, falls back to global config or empty widget,
141
+
-`RequestWidgetBuilder<TOut>? onRefreshing` - use it to show a widget while refreshing with previous data still available. If not provided, falls back to `onSuccess`.
106
142
107
-
Other than builders, you also need to provide the cubit based on which the `RequestCubitBuilder` will be rebuilt. And you can also pass `onErrorCallback` which allows you to pass a callback to error widget builder. You may want to use it to implement retry button.
143
+
Other than builders, you also need to provide the cubit based on which the `RequestCubitBuilder` will be rebuilt. You can also pass `onErrorCallback` which allows you to pass a callback to error widget builder. You may want to use it to implement a retry button.
108
144
109
145
Example usage of `RequestCubitBuilder`:
110
146
```dart
@@ -135,10 +171,31 @@ RequestCubitBuilder(
135
171
},
136
172
);
137
173
},
174
+
onRefreshing: (context, data) {
175
+
return Stack(
176
+
children: [
177
+
ListView.builder(
178
+
itemCount: data.assignments.length,
179
+
itemBuilder: (context, index) {
180
+
final assignment = data.assignments[index];
181
+
return ListTile(
182
+
title: AppText(assignment.id),
183
+
);
184
+
},
185
+
),
186
+
const Positioned(
187
+
top: 0,
188
+
left: 0,
189
+
right: 0,
190
+
child: LinearProgressIndicator(),
191
+
),
192
+
],
193
+
);
194
+
},
138
195
)
139
196
```
140
197
141
-
As you may see `onInitial`, `onLoading` and `onError` are marked as optional parameter. In many projects each of those widgets are the same for each page. So in order to eliminate even more boilerplate code, instead of passing them all each time you want to use `RequestCubitBuilder`, you can define them globally and provide in the whole app using [`RequestLayoutConfigProvider`](#requestlayoutconfigprovider).
198
+
As you may see `onLoading`, `onEmpty` and `onError` are marked as optional parameters. In many projects each of those widgets are the same for each page. So in order to eliminate even more boilerplate code, instead of passing them all each time you want to use `RequestCubitBuilder`, you can define them globally and provide in the whole app using [`RequestLayoutConfigProvider`](#requestlayoutconfigprovider).
0 commit comments