Closed burhankhanzada closed 1 year ago
flutter --version Flutter 3.7.8 • channel stable • https://github.com/flutter/flutter.git Framework • revision 90c64ed42b (12 days ago) • 2023-03-21 11:27:08 -0500 Engine • revision 9aa7816315 Tools • Dart 2.19.5 • DevTools 2.20.1 base_response.dart @JsonSerializable( genericArgumentFactories: true, ) class BaseResponse<T> { BaseResponse({ required this.status, required this.message, this.data, });
flutter --version
Flutter 3.7.8 • channel stable • https://github.com/flutter/flutter.git Framework • revision 90c64ed42b (12 days ago) • 2023-03-21 11:27:08 -0500 Engine • revision 9aa7816315 Tools • Dart 2.19.5 • DevTools 2.20.1
base_response.dart
@JsonSerializable( genericArgumentFactories: true, ) class BaseResponse<T> { BaseResponse({ required this.status, required this.message, this.data, });
factory BaseResponse.fromJson( Map<String, dynamic> json, T Function(Object? json) fromJsonT, ) => _$BaseResponseFromJson(json, fromJsonT);
Map<String, dynamic> toJson(Map<String, dynamic> Function(T value) toJsonT) => _$BaseResponseToJson(this, toJsonT);
@JsonKey(name: 'status') bool status;
@JsonKey(name: 'message') String message;
@JsonKey(name: 'data') T? data;
> read_notifications_data.dart ```dart @JsonSerializable() class ReadNotificationData { ReadNotificationData({this.notificationAvaliable}); factory ReadNotificationData.fromJson(Map<String, dynamic> json) => _$ReadNotificationDataFromJson(json); bool? notificationAvaliable; }
repository.dart Future<ReadNotificationResponse> readNotificationApi( int notificationId, ) async { final response = await ApiBaseHelper.httpRequest<ReadNotificationData>( endPoint: EndPoints.readNotifications, requestType: HttpRequest.get, params: notificationId.toString(), fromJsonT: ReadNotificationData.fromJson, );
repository.dart
Future<ReadNotificationResponse> readNotificationApi( int notificationId, ) async { final response = await ApiBaseHelper.httpRequest<ReadNotificationData>( endPoint: EndPoints.readNotifications, requestType: HttpRequest.get, params: notificationId.toString(), fromJsonT: ReadNotificationData.fromJson, );
return BaseResponse<ReadNotificationData>.fromJson( response, (json) => ReadNotificationData.fromJson(json), // The argument type Object? can't be assigned to the paramter type Map<String, dynamic> );
}
You need to cast json as Map<String, dynamic>
json as Map<String, dynamic>
factory BaseResponse.fromJson( Map<String, dynamic> json, T Function(Object? json) fromJsonT, ) => _$BaseResponseFromJson(json, fromJsonT);
Map<String, dynamic> toJson(Map<String, dynamic> Function(T value) toJsonT) => _$BaseResponseToJson(this, toJsonT);
@JsonKey(name: 'status') bool status;
@JsonKey(name: 'message') String message;
@JsonKey(name: 'data') T? data;
}