RestuRHP

baseState

Jan 8th, 2024
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 1.58 KB | None | 0 0
  1. enum StatusState { initial, loading, success, error }
  2.  
  3. class BaseState<T> {
  4.   StatusState status;
  5.   T? data;
  6.   dynamic message;
  7.  
  8.   BaseState({
  9.     this.status = StatusState.initial,
  10.     this.data,
  11.     this.message,
  12.   });
  13.  
  14.   BaseState<T> copyWith({
  15.     StatusState? status,
  16.     T? data,
  17.     dynamic message,
  18.   }) {
  19.     return BaseState<T>(
  20.       status: status ?? this.status,
  21.       data: data ?? this.data,
  22.       message: message ?? this.message,
  23.     );
  24.   }
  25.  
  26.   @override
  27.   String toString() {
  28.     return 'BaseState{status: $status, data: $data, message: $message}';
  29.   }
  30.  
  31. }
  32.  
  33.  
  34. BaseState<FareUpdateResponse> lrtFareState = BaseState<FareUpdateResponse>();
  35.  
  36. Future<void> getPriceNew({PostBuyTicketModel? data}) async {
  37.     lrtFareState = lrtFareState.copyWith(status: StatusState.loading);
  38.     notifyListeners();
  39.     try {
  40.       final response = await EndPoint.getFareQTS(
  41.         org: '${data?.origin}',
  42.         des: '${data?.destination}',
  43.         ticketNo: '${data?.ticketNo}',
  44.       );
  45.       if (response['code'] == '00') {
  46.         lrtFareState = lrtFareState.copyWith(
  47.           status: StatusState.success,
  48.           data: FareUpdateResponse.fromJson(response),
  49.         );
  50.         notifyListeners();
  51.       } else {
  52.         lrtFareState = lrtFareState.copyWith(
  53.           status: StatusState.error,
  54.           message: response['message'],
  55.         );
  56.       }
  57.       notifyListeners();
  58.     } catch (e) {
  59.       lrtFareState = lrtFareState.copyWith(
  60.         status: StatusState.error,
  61.         message: e.toString(),
  62.       );
  63.       notifyListeners();
  64.     }
  65.   }
Advertisement
Add Comment
Please, Sign In to add comment