google / json_serializable.dart

Generates utilities to aid in serializing to/from JSON.
https://pub.dev/packages/json_serializable
BSD 3-Clause "New" or "Revised" License
1.55k stars 395 forks source link

Map<String, dynamic> can't be assigned to Object #1302

Closed burhankhanzada closed 1 year ago

burhankhanzada commented 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,
});

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,
);
return BaseResponse<ReadNotificationData>.fromJson(
  response,
  (json) => ReadNotificationData.fromJson(json), // The argument type Object? can't be assigned to the paramter type Map<String, dynamic>
);

}

kevmoo commented 1 year ago

You need to cast json as Map<String, dynamic>