Created
August 22, 2024 21:45
-
-
Save WojuadeAA/01ca35010e3e856fc0103da1b86f6b0b to your computer and use it in GitHub Desktop.
network_request_state.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'package:freezed_annotation/freezed_annotation.dart'; | |
| part 'request_state.freezed.dart'; | |
| /// Example demonstrating the usage of the `RequestState` class with Riverpod and `Notifier`. | |
| /// | |
| /// This example shows how to handle network requests using a custom `RequestState` class | |
| /// with different states (initial, loading, failed, and success) and Riverpod's `Notifier`. | |
| /// | |
| /// Usage: | |
| /// - Define a provider called `requestProvider` for handling network requests with the `RequestNotifier`. | |
| /// - Create a `RequestNotifier` class extending `Notifier<RequestState>` to manage the state. | |
| /// - Simulate network requests using the `fetchSomeData` method inside `RequestNotifier`. | |
| /// - Watch state changes and react accordingly using `ProviderContainer`. | |
| /// | |
| /// ```dart | |
| /// | |
| /// class RequestNotifier extends Notifier<RequestState<ResponseModel>> { | |
| /// final useCase = UseCase(); | |
| /// @override | |
| /// RequestState<ResponseModel> build() { | |
| /// return const RequestState.initial(); | |
| /// } | |
| /// | |
| /// //Initiate a network request and update the state. | |
| /// void makeRequest() async { | |
| /// state = const RequestState.loading(); | |
| /// final action = await useCase(params: params); | |
| /// action.fold( | |
| /// (l) { | |
| /// state = RequestState.failed(message: | |
| /// parseError(l.error!)); | |
| /// }, | |
| /// (r) => state = RequestState.success(responseModel: r.data!), | |
| /// ); | |
| /// } | |
| /// } | |
| /// } | |
| /// //A provider for the `RequestNotifier` class to be used within your application. | |
| /// final requestNotifierProvider = NotifierProvider<RequestNotifier, RequestState<ResponseModel>>(() => RequestNotifier(), | |
| /// void main() { | |
| /// final container = ProviderContainer(); | |
| /// | |
| /// final requestState = container.read(requestProvider.notifier); | |
| /// | |
| /// requestState.fetchSomeData(); | |
| /// | |
| /// container.listen<RequestState>(requestProvider, (state) { | |
| /// if (state is RequestState.success) { | |
| /// LoggerService().logError("Data received: ${state.responseModel}"); | |
| /// } else if (state is RequestState.failed) { | |
| /// print("Request failed: ${state.message}"); | |
| /// } else if (state is RequestState.loading) { | |
| /// print("Request in progress..."); | |
| /// } else if (state is RequestState.initial) { | |
| /// print("Initial state"); | |
| /// } | |
| /// }); | |
| /// } | |
| /// ``` | |
| /// | |
| /// In this example, we use Riverpod and `Notifier` to manage and react to network request states | |
| /// represented by the `RequestState` class. You can replace the simulated network request with your | |
| /// actual API calls as needed. | |
| @freezed | |
| class RequestState<T> with _$RequestState<T> { | |
| const factory RequestState.initial() = _initial<T>; | |
| const factory RequestState.loading() = _loading<T>; | |
| const factory RequestState.failed({String? message}) = _failed<T>; | |
| const factory RequestState.success({required T responseModel}) = _success<T>; | |
| @override | |
| String toString() { | |
| return when( | |
| initial: () => "Initial", | |
| loading: () => "Loading", | |
| failed: (message) { | |
| return "Failed with $message"; | |
| }, | |
| success: (responseModel) { | |
| return "Succes with $responseModel"; | |
| }, | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment