Hi guys I have an api I use that responds like this: responds
{
"RAW": {
"BTC": {
"USD": {
"TYPE": "5",
"MARKET": "CCCAGG",
"FROMSYMBOL": "BTC",
"TOSYMBOL": "USD",
"FLAGS": "1026",
"PRICE": 41091.49,
"LASTUPDATE": 1649873443,
"MEDIAN": 41085.18,
"LASTVOLUME": 0.001056,
"LASTVOLUMETO": 43.38595008,
Now to convert and use it in darts (modeling) we have to do the mapping operation, right? Does anyone know how to do this when this response is nested! I did it, but I know it's not right
class CryptoEntity {
final String basecode;
// final String targetcod;
final int price;
final int volume;
final int change;
CryptoEntity.fromjason(Map<String, dynamic> jason)
: basecode = jason['FROMSYMBOL'],
// targetcod = jason['target'],
price = jason['PRICE'],
volume = jason['VOLUMEDAY'],
change = jason['LASTVOLUME'];
}
CodePudding user response:
try this:
class CryptoEntity {
final String? basecode; // ? => means that this field can be null
// final String targetcod;
final int? price;
final int? volume;
final int? change;
//constroctor
CryptoEntity(this.basecode, this.price, .. );
factory CryptoEntity.fromjason(Map<String, dynamic> jason){
return CryptoEntity(
price = jason['PRICE'],
volume = jason['VOLUMEDAY'],
change = jason['LASTVOLUME'];
);
}
}
CodePudding user response:
You can turn the json into an object
import 'dart:convert';
MyModel myModelFromMap(String str) => MyModel.fromMap(json.decode(str));
String myModelToMap(MyModel data) => json.encode(data.toMap());
class MyModel {
MyModel({
required this.raw,
});
Raw raw;
factory MyModel.fromMap(Map<String, dynamic> json) => MyModel(
raw: Raw.fromMap(json["RAW"]),
);
Map<String, dynamic> toMap() => {
"RAW": raw.toMap(),
};
}
class Raw {
Raw({
required this.btc,
});
Btc btc;
factory Raw.fromMap(Map<String, dynamic> json) => Raw(
btc: Btc.fromMap(json["BTC"]),
);
Map<String, dynamic> toMap() => {
"BTC": btc.toMap(),
};
}
class Btc {
Btc({
required this.usd,
});
Usd usd;
factory Btc.fromMap(Map<String, dynamic> json) => Btc(
usd: Usd.fromMap(json["USD"]),
);
Map<String, dynamic> toMap() => {
"USD": usd.toMap(),
};
}
class Usd {
Usd({
required this.type,
required this.market,
required this.fromsymbol,
required this.tosymbol,
required this.flags,
required this.price,
required this.lastupdate,
required this.median,
required this.lastvolume,
required this.lastvolumeto,
});
String type;
String market;
String fromsymbol;
String tosymbol;
String flags;
double price;
int lastupdate;
double median;
double lastvolume;
double lastvolumeto;
factory Usd.fromMap(Map<String, dynamic> json) => Usd(
type: json["TYPE"],
market: json["MARKET"],
fromsymbol: json["FROMSYMBOL"],
tosymbol: json["TOSYMBOL"],
flags: json["FLAGS"],
price: json["PRICE"].toDouble(),
lastupdate: json["LASTUPDATE"],
median: json["MEDIAN"].toDouble(),
lastvolume: json["LASTVOLUME"].toDouble(),
lastvolumeto: json["LASTVOLUMETO"].toDouble(),
);
Map<String, dynamic> toMap() => {
"TYPE": type,
"MARKET": market,
"FROMSYMBOL": fromsymbol,
"TOSYMBOL": tosymbol,
"FLAGS": flags,
"PRICE": price,
"LASTUPDATE": lastupdate,
"MEDIAN": median,
"LASTVOLUME": lastvolume,
"LASTVOLUMETO": lastvolumeto,
};
}
You can use it like this
MyModel myModel = myModelFromMap(jsonDecode(json));
CodePudding user response:
- Use freezed package and json serializable for code generation, it is good for the long term development but this requires basic knowledge in flutter.
- Use Online JSON to Dart converter, this site generates null safety dart class. With this solution, all you need is to change the class names after each conversion manually, and note for the
fromJson
factory constructor method andtoJson
class method.
CodePudding user response:
Your code should reflect the json.
class RAW {
Btc btc;
}
class Btc {
Usd usd;
}
class Usd {
....
}
It should process the nested json structures into the appropriate objects.