Good afternoon, I may have found a small error, but I can't figure it out until now.. I have the following class in dart, the purpose of this class is to receive json and transform each field to object
class Attributes {
final String lpn; //itemId
final String type; //itemType (REEL,FEEDER,ETC)
final String feederContainer; //containerId
final String feederSide; //locationInContainer(A o B)
final String locationInTool; //locationInTool
Attributes(
{required this.lpn,
required this.type,
required this.feederContainer,
required this.feederSide,
required this.locationInTool});
factory Attributes.fromJson(Map json) {
return Attributes(
lpn: json['itemId'],
type: json['itemType'],
feederContainer: json['containerId'],
feederSide: json['locationInContainer'],
locationInTool: json['locationInTool'],
);
}
}
class PartNumberAt {
final String partNumber; //partNumber
final String quantity; //quantity
final String initialQuantity; //initialQuantity
PartNumberAt(
{required this.partNumber,
required this.quantity,
required this.initialQuantity});
factory PartNumberAt.fromJson(Map json) {
return PartNumberAt(
partNumber: json['partNumber'],
quantity: json['quantity'],
initialQuantity: json['initialQuantity'],
);
}
}
//partnumber RawMaterial
class ReelPartNumber {
final PartNumberAt partNumberAt;
ReelPartNumber({required this.partNumberAt});
factory ReelPartNumber.fromJson(Map json) {
return ReelPartNumber(
partNumberAt: PartNumberAt.fromJson(json['attributes']),
);
}
}
class ReelLpn {
Attributes? attributes;
ReelPartNumber? reelPartNumber;
ReelLpn(
{required Attributes attributes, required ReelPartNumber reelPartNumber});
factory ReelLpn.fromJson(Map json) {
return ReelLpn(
attributes: Attributes.fromJson(json['attributes']),
reelPartNumber: ReelPartNumber.fromJson(json['RawMaterial']),
);
}
}
and I have a file where I make http requests, the request returns the following
{
"attributes": {
"itemId": "0605007783",
"itemKey": "14992663",
"itemType": "REEL",
"itemTypeClass": "Component Lot",
"containerId": "FA0210AEF424292",
"locationInContainer": "B",
"toolContainerId": "SMT6",
"locationInTool": "10004-B",
"quarantineLocked": "false",
"expired": "false",
"initTmst": "2022-01-20T09:40:30.969-03:00"
},
"RawMaterial": {
"attributes": {
"partNumber": "11201312001166",
"partNumberDesc": "",
"supplierId": "DEFAULT",
"quantity": "2497.0",
"initialQuantity": "5000.0",
"rejectedQuantity": "3.0",
"manualAdjustmentQuantity": "548.0"
},
}
and the request is made as follows
Future<ReelLpn?> getReelData(String lpn) async {
http.Response response = await http.get(Uri.parse(apiUrl '/$lpn'));
if (response.statusCode == 200) {
Map data = (json.decode(response.body)); //conver to json
print(data);
ReelLpn reelLpn = ReelLpn.fromJson(data); //transform json to ReelLpn
return reelLpn;
}
return null;
}
and I call the service as follows
ReelLpn? data = await CogiscanService().getReelData('0605007783');
print(data?.attributes?.type);
my problem starts there, when I print
print(data?.attributes?.type);
it returns null, I put several prints, in the ReelL class, Attributes and PartNumber, to see if I was reading the Map correctly, and they definitely read correctly.
So why when I want to access any of the fields does it return null?
CodePudding user response:
Change your ReelLpn Constructor. you are not referencing the class members... thats why its always null.
ReelLpn({required this.attributes, required this.reelPartNumber});