I'm trying to define UnsignedInt? id
in my data model. But it keeps giving me this error when I am trying to send the model to the server.
[SEVERE] json_serializable:json_serializable on lib/src/model/otp.dart (cached):
Could not generate
fromJson
code forcountryID
. To support the typeUnsignedInt
you can:
- Use
JsonConverter
https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonConverter-class.html- Use
JsonKey
fieldsfromJson
andtoJson
https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonKey/fromJson.html https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonKey/toJson.html package:myapp/src/model/otp.dart:18:16 ╷ 18 │ UnsignedInt? countryID; │ ^^^^^^^^^ ╵ [SEVERE] Failed after 103ms pub finished with exit code 1
Here is my code where I want to send the model to server:
Future<OTP?> getCode(String countryCode, String phone) async {
const url = HttpUtils.baseUrl HttpUtils.getcode;
_myOtp = null;
try {
final response = await http.post(
Uri.parse(url),
headers: {"Content-Type": "application/json"},
body: json.encode(OTP(phoneNo: phone,).toJson()),
);
_myOtp = OTP.fromJson(json.decode(response.body)["otp"]);
} catch (error) {
if (kDebugMode) {
print(error.toString());
}
}
return _myOtp;
}
CodePudding user response:
UnsignedInt
is not supported.
Out of the box, json_serializable supports many common types in the dart:core library: BigInt, bool, DateTime, double, Duration, Enum, int, Iterable, List, Map, num, Object, Set, String, Uri
See json_serializable for details.