@JsonKey Arguments of a constant creation must be constant expressions

Viewed 149

i am trying to make MainResponse class which is returned from backend with same format. I have created a Data class and the object it has should be generic json key regarding the class name like below:

import 'package:json_annotation/json_annotation.dart';
import 'package:style/api/response/status_response.dart';

part 'data.g.dart';

@JsonSerializable(genericArgumentFactories: true)
class Data<T> {

  @JsonKey(name: T.toString())
  T? returnedObject;
}

what I wanted to do is giving JsonKey name value as class name. but i am receiving following error:

Arguments of a constant creation must be constant expressions.

is there a way to achieve it?

1 Answers

sorry, the question was easy. just try to edit classname.g.dart file which is auto generated. and use'T.toString().toLowerCase()'instead of auto generated key.

Related