Reputation: 294
Im fetching data from api, but now that im using some other api that has a map inside a map witch im having a problem accessing, here is how the data looks.
{
"status": "string",
"name": {
"location": "string",
"startTime": "2022-01-31T08:13:21.027Z",
"endTime": "2022-01-31T08:13:21.027Z",
"duration": "string"
}
}
Im trying to access location.
class WorkingLocationStatus {
final String status;
//final String location;
WorkingLocationStatus({
required this.status,
// required this.location
});
factory WorkingLocationStatus.fromJson(Map<String, dynamic> json) {
return WorkingLocationStatus(
status: json['status'],
//location: json['location']
);
}
}
body: FutureBuilder<Response>(
future: futureDataForStatus,
builder: (context, snapshot) {
if (snapshot.hasData) {
WorkingLocationStatus data4 = WorkingLocationStatus.fromJson(
json.decode(snapshot.data!.body),
);
return Center(
child: Text(data4.status),
);
} else if (snapshot.hasError) {
return Text('${snapshot.error}');
}
return const Center(child: CircularProgressIndicator());
},
),
Upvotes: 1
Views: 2809
Reputation: 599
You will follow this basic step to implemented in your way
import 'dart:convert';
var jsonObj = """{
"status": "string",
"name": {
"location": "string",
"startTime": "2022-01-31T08:13:21.027Z",
"endTime": "2022-01-31T08:13:21.027Z",
"duration": "string"
}
}""";
void main(){
var resultBody = jsonDecode(jsonObj);
print("Here is your location: " + resultBody["name"]["location"]);
}
**Your Result like this **
Here is your location: string
Upvotes: 0
Reputation: 7308
To access the "location"
property you will first need to access it from the field "name"
:
class WorkingLocationStatus {
final String status;
final String location;
WorkingLocationStatus({
required this.status,
required this.location
});
factory WorkingLocationStatus.fromJson(Map<String, dynamic> json) {
return WorkingLocationStatus(
status: json['status'] as String,
location: json['name']['location'] as String,
);
}
}
Try the full code example on DartPad
Upvotes: 2
Reputation: 1178
You can use a json to dart tool to make model class from raw json.
Here is the full code.
// To parse this JSON data, do
//
// final workingLocationStatus = workingLocationStatusFromMap(jsonString);
import 'dart:convert';
class WorkingLocationStatus {
WorkingLocationStatus({
this.status,
this.name,
});
final String status;
final Name name;
factory WorkingLocationStatus.fromJson(String str) => WorkingLocationStatus.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory WorkingLocationStatus.fromMap(Map<String, dynamic> json) => WorkingLocationStatus(
status: json["status"],
name: Name.fromMap(json["name"]),
);
Map<String, dynamic> toMap() => {
"status": status,
"name": name.toMap(),
};
}
class Name {
Name({
this.location,
this.startTime,
this.endTime,
this.duration,
});
final String location;
final DateTime startTime;
final DateTime endTime;
final String duration;
factory Name.fromJson(String str) => Name.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory Name.fromMap(Map<String, dynamic> json) => Name(
location: json["location"],
startTime: DateTime.parse(json["startTime"]),
endTime: DateTime.parse(json["endTime"]),
duration: json["duration"],
);
Map<String, dynamic> toMap() => {
"location": location,
"startTime": startTime.toIso8601String(),
"endTime": endTime.toIso8601String(),
"duration": duration,
};
}
body: FutureBuilder<Response>(
future: futureDataForStatus,
builder: (context, snapshot) {
if (snapshot.hasData) {
WorkingLocationStatus data4 = WorkingLocationStatus.fromJson(
json.decode(snapshot.data!.body),
);
return Center(
child: Text(data4.name.location),
);
} else if (snapshot.hasError) {
return Text('${snapshot.error}');
}
return const Center(child: CircularProgressIndicator());
},
),
Upvotes: 1