You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Sale.fromJson(s)
factorySale.fromJson(Map json) =>Sale(
id: json['id'],
quantity: json['quantity'],
// eg. quantity: double.tryParse("${json['quantity']}") // this way it can convert String, int, num to double so you don't have to worry about the api data type
unitPrice: json['unit_price'],
createdAt:switch (json['created_at']) {
DateTime value => value,
String value =>DateTime.parse(value),
_ => json['created_at']
},
);
in this example the Map json data is coming from an api but the quantity is either double or int but the model's quantity field only accepts double. can we parse and cast it to double or are there any other available methods to deal with. i could cast every field but it would be if we have this feature inside the model instead of all over my project
The text was updated successfully, but these errors were encountered:
in this example the
Map json
data is coming from an api but thequantity
is eitherdouble
orint
but the model's quantity field only acceptsdouble
. can we parse and cast it todouble
or are there any other available methods to deal with. i could cast every field but it would be if we have this feature inside the model instead of all over my projectThe text was updated successfully, but these errors were encountered: