hi I am trying to retrieve nestedJson to dart, mainly Ill be working with API call which has similar data nested Json so this below is example I was trying if it works. so my main problem is I am not getting Expected output. below mentioned are my code files, if this seems wrong or tedius way, kindly guide me the right way to do it.
EXPECTED OUTPUT: | OUTPUT I GET: |
---|---|
dastagir | dastagir |
34 | 34 |
Shangai | Shangai |
[] | [] |
============ | ============ |
china | |
Bangladesh |
hi I am trying to retrieve nestedJson to dart, mainly Ill be working with API call which has similar data nested Json so this below is example I was trying if it works. so my main problem is I am not getting Expected output. below mentioned are my code files, if this seems wrong or tedius way, kindly guide me the right way to do it.
EXPECTED OUTPUT: | OUTPUT I GET: |
---|---|
dastagir | dastagir |
34 | 34 |
Shangai | Shangai |
[] | [] |
============ | ============ |
china | |
Bangladesh |
main.dart
void main() {
var myMap = {
"name": "dastagir",
"age": 34,
"city":"Shangai",
"address": [
{ // Map 1
"country": "china",
"city": "Shanghai"
},
{ // Map 2
"country": "Bangaldesh",
"city": "Dhaka"
},
]
};
var obj = Person.fromJson(myMap);
print(obj.name);
print(obj.age);
print(obj.city);
print(obj.address);
print("=======================================");
var myAddress = obj.address;
myAddress!.map((e){
print(e.country);
}).toList();
}
nested_json.dart
class Person{
String? name;
int? age;
String? city;
List<Address>? address;
Person({this.name, this.age, this.city, this.address});
Person.fromJson(Map<String, dynamic> json){
name = json['name'];
age = json['age'];
city = json['city'];
if(json['address']!=null){
address=<Address>[];
}
else{
(json['address'] as List).forEach((e){
address!.add(Address.fromJson(e));
});
}
}
}
// this is for inner Json List
class Address{
String? country;
String? city;
Address({this.country, this.city});
Address.fromJson(Map<String, dynamic> json){
country = json['country'];
city = json['city'];
}
}`
Share
Improve this question
asked Mar 6 at 17:30
appdevappdev
159 bronze badges
3
|
2 Answers
Reset to default 3You're using print
function in map
, but print
returns void
. map
is used for mapping one type into another, not for void action.
myAddress!.map((e){
print(e.country);
}).toList();
}
It's better to use forEach
myAddress?.forEach((e) => print(e));
or for loop
for (final address in (myAddress ?? const []) {
print(address);
}
[Extracting the answer from the comments for better visibility...]
I think your error is here: json['address']!=null
. If you have items, you're setting address to empty list, but then never adding the individual addresses. If you have no items, that code is likely to blow up.
The solution is to fix the boolean conditions so they do the right thing. Initialize with an empty list, and for each item present, add that to the list.
json['address']!=null
. If you have items, you're setting address to empty list, but then never adding the individual addresses. If you have no items, that code is likely to blow up. – Randal Schwartz Commented Mar 6 at 19:10