Parsing JSON Object with unique key in Dart - json

I'm having a hard time figuring out how to parse a JSON response that has unique keys into nested object.
I've done some research and came across this article (https://medium.com/flutter-community/parsing-complex-json-in-flutter-747c46655f51) but it doesn't give an example of a JSON object with unique/random keys.
See below for the json response I get from the backend. The student usernames are unique (i.e. john354, kim553, etc) and a new student could get added at any time. Could someone give me an example of what my model should look like for this type of structure and how to display the data with listview.builder? Thank you so much!
{
"school": {
"students": {
"john354": {
"fullName": "John Kindle",
"permissions": {
"permission1": "here",
"permission2": "here"
}
},
"kim553": {
"fullName": "Kim Johnson"
"permissions": {
"permission1": "here",
"permission2": "here"
}
},
"dory643": {
"fullName": "Dory Thomas"
"permissions": {
"permission1": "here",
"permission2": "here"
}
}
},
"footer": "Text goes here"
},
"status": {
"state": "open"
}
}
Attempt Data Model
class School {
School school;
Status status;
School({this.school, this.status});
School.fromJson(Map<String, dynamic> json) {
school =
json['school'] != null ? new School.fromJson(json['school']) : null;
status =
json['status'] != null ? new Status.fromJson(json['status']) : null;
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.school != null) {
data['school'] = this.school.toJson();
}
if (this.status != null) {
data['status'] = this.status.toJson();
}
return data;
}
}
class School {
Students students;
String footer;
School({this.students, this.footer});
School.fromJson(Map<String, dynamic> json) {
students = json['students'] != null
? new Students.fromJson(json['students'])
: null;
footer = json['footer'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.students != null) {
data['students'] = this.students.toJson();
}
data['footer'] = this.footer;
return data;
}
}
class Students {
final String username;
final Info info;
Options({this.username,this.info});
factory Students.fromJson(String name, Map<String, dynamic> json){
return Students(
username: username,
info: Info(
fullName: json['info']['fullName']
)
);
}
}
class Info {
String fullName;
Permissions permissions;
Info({this.fullName, this.permissions});
Info.fromJson(Map<String, dynamic> json) {
fullName = json['fullName'];
permissions = json['permissions'] != null
? new Permissions.fromJson(json['permissions'])
: null;
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['fullName'] = this.fullName;
if (this.permissions != null) {
data['permissions'] = this.permissions.toJson();
}
return data;
}
}
class Permissions {
String permission1;
String permission2;
Permissions({this.permission1, this.permission2});
Permissions.fromJson(Map<String, dynamic> json) {
permission1 = json['permission1'];
permission2 = json['permission2'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['permission1'] = this.permission1;
data['permission2'] = this.permission2;
return data;
}
}
class Status {
String state;
Status({this.state});
Status.fromJson(Map<String, dynamic> json) {
state = json['state'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['state'] = this.state;
return data;
}
}

To simplify JSON pursing you can use app.quicktype.io to convert JSON to DART.
try the Following Data Model,
import 'dart:convert';
School schoolFromJson(String str) => School.fromJson(json.decode(str));
String schoolToJson(School data) => json.encode(data.toJson());
class School {
School({
this.school,
this.status,
});
final SchoolClass school;
final Status status;
factory School.fromJson(Map<String, dynamic> json) => School(
school: SchoolClass.fromJson(json["school"]),
status: Status.fromJson(json["status"]),
);
Map<String, dynamic> toJson() => {
"school": school.toJson(),
"status": status.toJson(),
};
}
class SchoolClass {
SchoolClass({
this.students,
this.footer,
});
final Map<String, Student> students;
final String footer;
factory SchoolClass.fromJson(Map<String, dynamic> json) => SchoolClass(
students: Map.from(json["students"]).map((k, v) => MapEntry<String, Student>(k, Student.fromJson(v))),
footer: json["footer"],
);
Map<String, dynamic> toJson() => {
"students": Map.from(students).map((k, v) => MapEntry<String, dynamic>(k, v.toJson())),
"footer": footer,
};
}
class Student {
Student({
this.fullName,
this.permissions,
});
final String fullName;
final Permissions permissions;
factory Student.fromJson(Map<String, dynamic> json) => Student(
fullName: json["fullName"],
permissions: Permissions.fromJson(json["permissions"]),
);
Map<String, dynamic> toJson() => {
"fullName": fullName,
"permissions": permissions.toJson(),
};
}
class Permissions {
Permissions({
this.permission1,
this.permission2,
});
final String permission1;
final String permission2;
factory Permissions.fromJson(Map<String, dynamic> json) => Permissions(
permission1: json["permission1"],
permission2: json["permission2"],
);
Map<String, dynamic> toJson() => {
"permission1": permission1,
"permission2": permission2,
};
}
class Status {
Status({
this.state,
});
final String state;
factory Status.fromJson(Map<String, dynamic> json) => Status(
state: json["state"],
);
Map<String, dynamic> toJson() => {
"state": state,
};
}
To parse this JSON data, do
final school = schoolFromJson(jsonString);

Related

convert it to list flutter firebase

this is my database image
import 'dart:convert';
class ReviewcountInfo{
List<String> uid;
ReviewcountInfo({
required this.uid,
})
List<String> toList() {
return
}
factory ReviewcountInfo.fromMap(Map<String, dynamic> map) {
return ReviewcountInfo(
uid: ['uid'] ?? 0,
);}
String toJson() => json.encode(toList());
factory ReviewcountInfo.fromJson(String source) =>
ReviewcountInfo.fromMap(json.decode(source));
}
I want to get data in the form of an array from firebase, how can I get it?
I don't know how to fill 'List toList()','factory ReviewcountInfo' part.
class BrandingInfo {
num price;
BrandingInfo({
required this.price,
});
Map<String, dynamic> toMap() {
return {
'price': price,
};
}
factory BrandingInfo.fromMap(Map<String, dynamic> map) {
return BrandingInfo(
price: map['price'] ?? 0,
);
}
String toJson() => json.encode(toMap());
factory BrandingInfo.fromJson(String source) =>
BrandingInfo.fromMap(json.decode(source));
}
this is my example Code
I am coding by referring to the code above.
try this model
import 'dart:convert';
List<ReviewCount> reviewCountFromJson(String str) => List<ReviewCount>.from(json.decode(str).map((x) => ReviewCount.fromJson(x)));
String reviewCountToJson(List<ReviewCount> data) => json.encode(List<dynamic>.from(data.map((x) => x.toJson())));
class ReviewCount {
ReviewCount({
this.reviewcount,
});
final Reviewcount? reviewcount;
factory ReviewCount.fromJson(Map<String, dynamic> json) => ReviewCount(
reviewcount: json["reviewcount"] == null ? null : Reviewcount.fromJson(json["reviewcount"]),
);
Map<String, dynamic> toJson() => {
"reviewcount": reviewcount == null ? null : reviewcount.toJson(),
};
}
class Reviewcount {
Reviewcount({
this.uid,
});
final List<String>? uid;
factory Reviewcount.fromJson(Map<String, dynamic> json) => Reviewcount(
uid: json["uid"] == null ? null : List<String>.from(json["uid"].map((x) => x)),
);
Map<String, dynamic> toJson() => {
"uid": uid == null ? null : List<dynamic>.from(uid!.map((x) => x)),
};
}
then for getting the data from firestore
Future<Reviewcount> getuidList({String? pathDocuid}) async{
final response = await FirebaseFirestore.instance.collection("service").doc(pathDocuid!).get();
final mapItFirst = response.data() as Map<String,dynamic>;
// but first print the data using log so you can see if it
// gettingdata
log("${mapItFirst['reviewcount']}");
log("${mapItFirst['reviewcount']['uid']}");
return Reviewcount.fromJson(mapItFirst["reviewcount"]);
}
try this if it works if not mention me.

Why can't i get data value from json - flutter

Am trying to get values from my json file categories.json using flutter but am always getting error or its not showing and I don't really know what is wrong
This is my main.dart
Future<List<Category>> loadData() async {
String jString = await rootBundle.loadString("assets/categories.json");
List<dynamic> jRes = jsonDecode(jString);
List<Category> datas = jRes.map((e) => Category.fromJson(e)).toList();
return datas;
}
Container(
child: FutureBuilder<List<Category>>(
future: loadData(),
builder: (context, data) {
if (data.connectionState != ConnectionState.waiting &&
data.hasData) {
var userList = data.data;
return ListView.builder(
itemCount: userList.length,
itemBuilder: (context, index) {
var userData = userList[index];
return Column(
children: [
Text("Category: ${userData.catName}"),
],
);
});
} else {
return Center(
child: CircularProgressIndicator(),
);
}
})),
and my model.dart
class Category {
String catId;
String catName;
SubCategory subcat;
Category({this.catId, this.catName, this.subcat});
factory Category.fromJson(Map<String, dynamic> json) {
return Category(
catId: json['cat_id'],
catName: json['category'],
subcat: SubCategory.fromJson(json['cat_subcategory']),
);
}
Map<String, dynamic> toJson() {
return {
"cat_id": catId,
"category": catName,
};
}
}
class SubCategory {
String subName, subImage;
SubCategory({this.subName, this.subImage});
factory SubCategory.fromJson(Map<String, dynamic> json) {
return SubCategory(subName: json['sub_name'], subImage: json['sub_image']);
}
Map<String, dynamic> toJson() {
return {
"sub_name": subName,
"sub_image": subImage,
};
}
}
and lastly my categories.json file
[{
"category": "Design & Creativity",
"cat_id": "1",
"cat_subcategory": [
{
"sub_name": "Ads",
"sub_image": "https://images.unsplash.com/photo-1589838017489-9198a27bd040?ixid=MXwxMjA3fDB8MHxzZWFyY2h8Mnx8YWR2ZXJ0aXNlbWVudHxlbnwwfHwwfA%3D%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=500&q=60"
}
]
}]
//There are more of them
The problem am facing when I run it is that it only shows me the CircularProgressIndicator() in my main.dart and when I remove the if statement, it says Another exception was thrown: NoSuchMethodError: The getter 'length' was called on null. Please how do I go about solving this problem and if you need more explanation then tell me
PS: When I check the loadData() value it says type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>'
EDIT: Thank you all for the answers but what i did was to generate a model using this website model generator
and i was able to get the value
Personally, when I want to load data from json file, I do this:
Future<List<Category>> loadData() async {
List<Category> datas = [];
return rootBundle.loadString("assets/categories.json").then((value) {
List<dynamic> jRes = jsonDecode(value);
jRes.forEach((element) {
datas.add(Category.fromJson(element));
});
return datas;
});
}
Also, in SubCategory class
factory SubCategory.fromJson(Map<String, dynamic> json) {
return SubCategory(subName: json['sub_name'], subImage: json['sub_image']);
}
Your fromJson() require Map<String, dynamic>.
But if you look at your file
"cat_subcategory": [
{
"sub_name": "Ads",
"sub_image": "https://images.unsplash.com/photo-1589838017489-9198a27bd040?ixid=MXwxMjA3fDB8MHxzZWFyY2h8Mnx8YWR2ZXJ0aXNlbWVudHxlbnwwfHwwfA%3D%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=500&q=60"
}
]
You can see that cat_subcategory is a List
So you are giving a List to your SubCategory.fromJson() instead of a Map.
If you want to give a Map, you can simply give the first index of your list
Your Category.fromJson() become
factory Category.fromJson(Map<String, dynamic> json) {
return Category(
catId: json['cat_id'],
catName: json['category'],
subcat: SubCategory.fromJson(json['cat_subcategory'][0]),
);
}
Change your model.dart class
class Category {
String category;
String catId;
List<CatSubcategory> catSubcategory;
Category({this.category, this.catId, this.catSubcategory});
Category.fromJson(Map<String, dynamic> json) {
category = json['category'];
catId = json['cat_id'];
if (json['cat_subcategory'] != null) {
catSubcategory = new List<CatSubcategory>();
json['cat_subcategory'].forEach((v) {
catSubcategory.add(new CatSubcategory.fromJson(v));
});
}}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['category'] = this.category;
data['cat_id'] = this.catId;
if (this.catSubcategory != null) {
data['cat_subcategory'] =
this.catSubcategory.map((v) => v.toJson()).toList();
}
return data;
}}
class CatSubcategory {
String subName;
String subImage;
CatSubcategory({this.subName, this.subImage});
CatSubcategory.fromJson(Map<String, dynamic> json) {
subName = json['sub_name'];
subImage = json['sub_image'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['sub_name'] = this.subName;
data['sub_image'] = this.subImage;
return data;
}}
And Replace
List<Category> datas = jRes.map((e) => Category.fromJson(e)).toList();
with
List<Category> datas = List<Category>.from(jRes.map((category)=> Category.fromJson(category)));

Unable to load json data from the internet due to a subtyp error : List<dynamic> is not type Map<String,Dymanic>

I'm new to flutter and I want to fetch json data from an API. I'm using json serializable.
this is my code where I fetch the data from the internet:
import 'dart:async';
import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:flutter_json_widget/flutter_json_widget.dart';
import './JsonDataMeter.dart';
Future<Meter> fetchMeter() async {
final response = await http.get(
'the url');
if (response.statusCode == 200) {
// If the server did return a 200 OK response,
// then parse the JSON.
return Meter.fromJson(jsonDecode(response.body));
} else {
// If the server did not return a 200 OK response,
// then throw an exception.
throw Exception('Failed to load meter');
}
}
class MeterList extends StatefulWidget {
MeterList({Key key, }) : super(key: key);
#override
_MeterList createState() => _MeterList();
}
class _MeterList extends State<MeterList> {
Future<Meter> futureMeter;
#override
void initState(){
super.initState();
futureMeter = fetchMeter();
}
#override
Widget build(BuildContext context) {
return FutureBuilder<Meter>(
future: futureMeter,
builder: (context, snapshot) {
if (snapshot.hasData) {
return Text(snapshot.data.address.city);
} else if (snapshot.hasError) {
//return Text("${snapshot.error}");
print(snapshot.error);
}
// By default, show a loading spinner.
return CircularProgressIndicator();
},
);
}
This is where I create the classes, which contain the json data:
import 'package:json_annotation/json_annotation.dart';
part 'JsonDataMeter.g.dart';
#JsonSerializable(explicitToJson: true)
class Meter{
Address address;
List<Apartments> apartments;
Meter({this.address, this.apartments});
factory Meter.fromJson(Map<String,dynamic> json) => _$MeterFromJson(json);
Map<String,dynamic> toJson() => _$MeterToJson(this);
}
#JsonSerializable()
class Address {
String streetAndNumber;
int PLZ;
String city;
Address({this.streetAndNumber, this.PLZ, this.city});
factory Address.fromJson(Map<String, dynamic> json) =>
_$AddressFromJson(json);
Map<String,dynamic> toJson() => _$AddressToJson(this);
}
#JsonSerializable(explicitToJson: true)
class Apartments{
Tenants tenants;
List<AssignedMeter> assignedMeter;
String id;
Meters meters;
String location;
int area;
String belongingProperty;
int v;
Apartments({this.tenants,this.assignedMeter, this.id, this.meters, this.location, this.area, this.belongingProperty, this.v});
factory Apartments.fromJson(Map<String,dynamic> json) => _$ApartmentsFromJson(json);
Map<String,dynamic> toJson() => _$ApartmentsToJson(this);
}
#JsonSerializable()
class Tenants{
String id;
String name;
String belongingApartment;
String timeInApartment;
int prepayment;
int v;
UsageTimes usageTimes;
AssociatedApartments associatedApartments;
Tenants({this.id,this.name, this.belongingApartment, this.timeInApartment, this.prepayment, this.v, this.usageTimes, this.associatedApartments});
factory Tenants.fromJson(Map<String,dynamic> json) => _$TenantsFromJson(json);
Map<String,dynamic> toJson() => _$TenantsToJson(this);
}
#JsonSerializable()
class UsageTimes{
String id;
String usageTime;
int usedArea;
UsageTimes({this.id, this.usageTime, this.usedArea});
factory UsageTimes.fromJson(Map<String,dynamic> json) => _$UsageTimesFromJson(json);
Map<String,dynamic> toJson() => _$UsageTimesToJson(this);
}
#JsonSerializable()
class AssociatedApartments{
UsageTimes usageTimes;
String id;
String apartmentID;
String timeInApartment;
AssociatedApartments({this.usageTimes, this.id, this.apartmentID, this.timeInApartment});
factory AssociatedApartments.fromJson(Map<String,dynamic> json) => _$AssociatedApartmentsFromJson(json);
Map<String,dynamic> toJson() => _$AssociatedApartmentsToJson(this);
}
#JsonSerializable()
class AssignedMeter{
String id;
MeterReadings meterReadings;
String belongingApartment;
String meterNumber;
String meterType;
int v;
AssignedMeter({this.id, this.meterReadings, this.belongingApartment, this.meterNumber, this.meterType, this.v});
factory AssignedMeter.fromJson(Map<String,dynamic> json) => _$AssignedMeterFromJson(json);
Map<String,dynamic> toJson() => _$AssignedMeterToJson(this);
}
#JsonSerializable()
class MeterReadings {
String id;
String date;
String value;
MeterReadings({this.id, this.date, this.value});
factory MeterReadings.fromJson(Map<String,dynamic> json) => _$MeterReadingsFromJson(json);
Map<String,dynamic> toJson() => _$MeterReadingsToJson(this);
}
#JsonSerializable()
class Meters{
String id;
String dateStart;
String dateEnd;
int waterStart;
int waterEnd;
int powerStart;
int powerEnd;
Meters({this.id, this.dateStart, this.dateEnd, this.waterStart, this.waterEnd, this.powerStart, this.powerEnd});
factory Meters.fromJson(Map<String,dynamic> json) => _$MetersFromJson(json);
Map<String,dynamic> toJson() => _$MetersToJson(this);
}
And this the auto generated code from JsonSerializable:
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'JsonDataMeter.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
Meter _$MeterFromJson(Map<String, dynamic> json) {
return Meter(
address: json['address'] == null
? null
: Address.fromJson(json['address'] as Map<String, dynamic>),
apartments: (json['apartments'] as List)
?.map((e) =>
e == null ? null : Apartments.fromJson(e as Map<String, dynamic>))
?.toList(),
);
}
Map<String, dynamic> _$MeterToJson(Meter instance) => <String, dynamic>{
'address': instance.address,
'apartments': instance.apartments,
};
Address _$AddressFromJson(Map<String, dynamic> json) {
return Address(
streetAndNumber: json['streetAndNumber'] as String,
PLZ: json['PLZ'] as int,
city: json['city'] as String,
);
}
Map<String, dynamic> _$AddressToJson(Address instance) => <String, dynamic>{
'streetAndNumber': instance.streetAndNumber,
'PLZ': instance.PLZ,
'city': instance.city,
};
Apartments _$ApartmentsFromJson(Map<String, dynamic> json) {
return Apartments(
tenants: json['tenants'] == null
? null
: Tenants.fromJson(json['tenants'] as Map<String, dynamic>),
assignedMeter: (json['assignedMeter'] as List)
?.map((e) => e == null
? null
: AssignedMeter.fromJson(e as Map<String, dynamic>))
?.toList(),
id: json['id'] as String,
meters: json['meters'] == null
? null
: Meters.fromJson(json['meters'] as Map<String, dynamic>),
location: json['location'] as String,
area: json['area'] as int,
belongingProperty: json['belongingProperty'] as String,
v: json['v'] as int,
);
}
Map<String, dynamic> _$ApartmentsToJson(Apartments instance) =>
<String, dynamic>{
'tenants': instance.tenants,
'assignedMeter': instance.assignedMeter,
'id': instance.id,
'meters': instance.meters,
'location': instance.location,
'area': instance.area,
'belongingProperty': instance.belongingProperty,
'v': instance.v,
};
Tenants _$TenantsFromJson(Map<String, dynamic> json) {
return Tenants(
id: json['id'] as String,
name: json['name'] as String,
belongingApartment: json['belongingApartment'] as String,
timeInApartment: json['timeInApartment'] as String,
prepayment: json['prepayment'] as int,
v: json['v'] as int,
usageTimes: json['usageTimes'] == null
? null
: UsageTimes.fromJson(json['usageTimes'] as Map<String, dynamic>),
associatedApartments: json['associatedApartments'] == null
? null
: AssociatedApartments.fromJson(
json['associatedApartments'] as Map<String, dynamic>),
);
}
Map<String, dynamic> _$TenantsToJson(Tenants instance) => <String, dynamic>{
'id': instance.id,
'name': instance.name,
'belongingApartment': instance.belongingApartment,
'timeInApartment': instance.timeInApartment,
'prepayment': instance.prepayment,
'v': instance.v,
'usageTimes': instance.usageTimes,
'associatedApartments': instance.associatedApartments,
};
UsageTimes _$UsageTimesFromJson(Map<String, dynamic> json) {
return UsageTimes(
id: json['id'] as String,
usageTime: json['usageTime'] as String,
usedArea: json['usedArea'] as int,
);
}
Map<String, dynamic> _$UsageTimesToJson(UsageTimes instance) =>
<String, dynamic>{
'id': instance.id,
'usageTime': instance.usageTime,
'usedArea': instance.usedArea,
};
AssociatedApartments _$AssociatedApartmentsFromJson(Map<String, dynamic> json) {
return AssociatedApartments(
usageTimes: json['usageTimes'] == null
? null
: UsageTimes.fromJson(json['usageTimes'] as Map<String, dynamic>),
id: json['id'] as String,
apartmentID: json['apartmentID'] as String,
timeInApartment: json['timeInApartment'] as String,
);
}
Map<String, dynamic> _$AssociatedApartmentsToJson(
AssociatedApartments instance) =>
<String, dynamic>{
'usageTimes': instance.usageTimes,
'id': instance.id,
'apartmentID': instance.apartmentID,
'timeInApartment': instance.timeInApartment,
};
AssignedMeter _$AssignedMeterFromJson(Map<String, dynamic> json) {
return AssignedMeter(
id: json['id'] as String,
meterReadings: json['meterReadings'] == null
? null
: MeterReadings.fromJson(json['meterReadings'] as Map<String, dynamic>),
belongingApartment: json['belongingApartment'] as String,
meterNumber: json['meterNumber'] as String,
meterType: json['meterType'] as String,
v: json['v'] as int,
);
}
Map<String, dynamic> _$AssignedMeterToJson(AssignedMeter instance) =>
<String, dynamic>{
'id': instance.id,
'meterReadings': instance.meterReadings,
'belongingApartment': instance.belongingApartment,
'meterNumber': instance.meterNumber,
'meterType': instance.meterType,
'v': instance.v,
};
MeterReadings _$MeterReadingsFromJson(Map<String, dynamic> json) {
return MeterReadings(
id: json['id'] as String,
date: json['date'] as String,
value: json['value'] as String,
);
}
Map<String, dynamic> _$MeterReadingsToJson(MeterReadings instance) =>
<String, dynamic>{
'id': instance.id,
'date': instance.date,
'value': instance.value,
};
Meters _$MetersFromJson(Map<String, dynamic> json) {
return Meters(
id: json['id'] as String,
dateStart: json['dateStart'] as String,
dateEnd: json['dateEnd'] as String,
waterStart: json['waterStart'] as int,
waterEnd: json['waterEnd'] as int,
powerStart: json['powerStart'] as int,
powerEnd: json['powerEnd'] as int,
);
}
Map<String, dynamic> _$MetersToJson(Meters instance) => <String, dynamic>{
'id': instance.id,
'dateStart': instance.dateStart,
'dateEnd': instance.dateEnd,
'waterStart': instance.waterStart,
'waterEnd': instance.waterEnd,
'powerStart': instance.powerStart,
'powerEnd': instance.powerEnd,
};
Finally this is the json I want to fetch (not the original values):
{
"address": {
"streetAndNumber": "SomeData",
"PLZ": 1,
"city": "SomeData"
},
"apartments": [
{
"tenants": [
{
"_id": "SomeData",
"name": "SomeData",
"belongingApartment": "SomeData",
"timeInApartment": "SomeData",
"prepayment": 1,
"__v": 1,
"usageTimes": [
{
"_id": "SomeData",
"usageTime": "SomeData",
"usedArea": 1
}
],
"associatedApartments": [
{
"usageTimes": [
{
"_id": "SomeData",
"usageTime": "SomeData",
"usedArea": 1
}
],
"_id": "SomeData",
"apartmentID": "SomeData",
"timeInApartment": "SomeData"
}
]
}
],
"assignedMeters": [
{
"_id": "SomeData",
"meterReadings": [
{
"_id": "SomeData",
"date": "SomeData",
"value": 1
}
],
"belongingApartment": "SomeData",
"meterNumber": "SomeData",
"meterType": "SomeData",
"__v": 1
},
{
"_id": "SomeData",
"meterReadings": [
{
"_id": "SomeData",
"date": "SomeData",
"value": 1
}
],
"belongingApartment": "SomeData",
"meterNumber": "SomeData",
"meterType": "SomeData",
"__v": 1
},
{
"_id": "SomeData",
"meterReadings": [
{
"_id": "SomeData",
"date": "SomeData",
"value": 1
},
{
"_id": "SomeData",
"date": "SomeData",
"value": 1
}
],
"belongingApartment": "SomeData",
"meterNumber": "SomeData",
"meterType": "SomeData",
"__v": 1
}
],
"_id": "SomeData",
"meters": [
{
"_id": "SomeData",
"dateStart": "SomeData",
"dateEnd": "SomeData",
"waterStart": 1,
"waterEnd": 1,
"powerStart": 1,
"powerEnd": 1
}
],
"location": "SomeData",
"area": 1,
"belongingProperty": "SomeData",
"__v": 1
},
I always get the Error:
I/flutter ( 4626): type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>' in type cast
As I said I'm new to flutter and it's maybe just a really stupid mistake but please help me, I feel like I tried everything but I just don't know what to do.
Thank you very much.
The issue lies in your Model.
e.g:
In apartment model tenants, meters should be type List<...>.
In tenants model usageTimes, associatedApartments should be type List<..>.
In associatedApartments model usageTimes should be type List<...>.
I have generated a new response model from your json using this link https://javiercbk.github.io/json_to_dart/.
Give it a try.
class MeterListResponse {
Address address;
List<Apartments> apartments;
MeterListResponse({this.address, this.apartments});
MeterListResponse.fromJson(Map<String, dynamic> json) {
address =
json['address'] != null ? new Address.fromJson(json['address']) : null;
if (json['apartments'] != null) {
apartments = new List<Apartments>();
json['apartments'].forEach((v) {
apartments.add(new Apartments.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.address != null) {
data['address'] = this.address.toJson();
}
if (this.apartments != null) {
data['apartments'] = this.apartments.map((v) => v.toJson()).toList();
}
return data;
}
}
class Address {
String streetAndNumber;
int pLZ;
String city;
Address({this.streetAndNumber, this.pLZ, this.city});
Address.fromJson(Map<String, dynamic> json) {
streetAndNumber = json['streetAndNumber'];
pLZ = json['PLZ'];
city = json['city'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['streetAndNumber'] = this.streetAndNumber;
data['PLZ'] = this.pLZ;
data['city'] = this.city;
return data;
}
}
class Apartments {
List<Tenants> tenants;
List<AssignedMeters> assignedMeters;
String sId;
List<Meters> meters;
String location;
int area;
String belongingProperty;
int iV;
Apartments(
{this.tenants,
this.assignedMeters,
this.sId,
this.meters,
this.location,
this.area,
this.belongingProperty,
this.iV});
Apartments.fromJson(Map<String, dynamic> json) {
if (json['tenants'] != null) {
tenants = new List<Tenants>();
json['tenants'].forEach((v) {
tenants.add(new Tenants.fromJson(v));
});
}
if (json['assignedMeters'] != null) {
assignedMeters = new List<AssignedMeters>();
json['assignedMeters'].forEach((v) {
assignedMeters.add(new AssignedMeters.fromJson(v));
});
}
sId = json['_id'];
if (json['meters'] != null) {
meters = new List<Meters>();
json['meters'].forEach((v) {
meters.add(new Meters.fromJson(v));
});
}
location = json['location'];
area = json['area'];
belongingProperty = json['belongingProperty'];
iV = json['__v'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.tenants != null) {
data['tenants'] = this.tenants.map((v) => v.toJson()).toList();
}
if (this.assignedMeters != null) {
data['assignedMeters'] =
this.assignedMeters.map((v) => v.toJson()).toList();
}
data['_id'] = this.sId;
if (this.meters != null) {
data['meters'] = this.meters.map((v) => v.toJson()).toList();
}
data['location'] = this.location;
data['area'] = this.area;
data['belongingProperty'] = this.belongingProperty;
data['__v'] = this.iV;
return data;
}
}
class Tenants {
String sId;
String name;
String belongingApartment;
String timeInApartment;
int prepayment;
int iV;
List<UsageTimes> usageTimes;
List<AssociatedApartments> associatedApartments;
Tenants(
{this.sId,
this.name,
this.belongingApartment,
this.timeInApartment,
this.prepayment,
this.iV,
this.usageTimes,
this.associatedApartments});
Tenants.fromJson(Map<String, dynamic> json) {
sId = json['_id'];
name = json['name'];
belongingApartment = json['belongingApartment'];
timeInApartment = json['timeInApartment'];
prepayment = json['prepayment'];
iV = json['__v'];
if (json['usageTimes'] != null) {
usageTimes = new List<UsageTimes>();
json['usageTimes'].forEach((v) {
usageTimes.add(new UsageTimes.fromJson(v));
});
}
if (json['associatedApartments'] != null) {
associatedApartments = new List<AssociatedApartments>();
json['associatedApartments'].forEach((v) {
associatedApartments.add(new AssociatedApartments.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['_id'] = this.sId;
data['name'] = this.name;
data['belongingApartment'] = this.belongingApartment;
data['timeInApartment'] = this.timeInApartment;
data['prepayment'] = this.prepayment;
data['__v'] = this.iV;
if (this.usageTimes != null) {
data['usageTimes'] = this.usageTimes.map((v) => v.toJson()).toList();
}
if (this.associatedApartments != null) {
data['associatedApartments'] =
this.associatedApartments.map((v) => v.toJson()).toList();
}
return data;
}
}
class UsageTimes {
String sId;
String usageTime;
int usedArea;
UsageTimes({this.sId, this.usageTime, this.usedArea});
UsageTimes.fromJson(Map<String, dynamic> json) {
sId = json['_id'];
usageTime = json['usageTime'];
usedArea = json['usedArea'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['_id'] = this.sId;
data['usageTime'] = this.usageTime;
data['usedArea'] = this.usedArea;
return data;
}
}
class AssociatedApartments {
List<UsageTimes> usageTimes;
String sId;
String apartmentID;
String timeInApartment;
AssociatedApartments(
{this.usageTimes, this.sId, this.apartmentID, this.timeInApartment});
AssociatedApartments.fromJson(Map<String, dynamic> json) {
if (json['usageTimes'] != null) {
usageTimes = new List<UsageTimes>();
json['usageTimes'].forEach((v) {
usageTimes.add(new UsageTimes.fromJson(v));
});
}
sId = json['_id'];
apartmentID = json['apartmentID'];
timeInApartment = json['timeInApartment'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.usageTimes != null) {
data['usageTimes'] = this.usageTimes.map((v) => v.toJson()).toList();
}
data['_id'] = this.sId;
data['apartmentID'] = this.apartmentID;
data['timeInApartment'] = this.timeInApartment;
return data;
}
}
class AssignedMeters {
String sId;
List<MeterReadings> meterReadings;
String belongingApartment;
String meterNumber;
String meterType;
int iV;
AssignedMeters(
{this.sId,
this.meterReadings,
this.belongingApartment,
this.meterNumber,
this.meterType,
this.iV});
AssignedMeters.fromJson(Map<String, dynamic> json) {
sId = json['_id'];
if (json['meterReadings'] != null) {
meterReadings = new List<MeterReadings>();
json['meterReadings'].forEach((v) {
meterReadings.add(new MeterReadings.fromJson(v));
});
}
belongingApartment = json['belongingApartment'];
meterNumber = json['meterNumber'];
meterType = json['meterType'];
iV = json['__v'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['_id'] = this.sId;
if (this.meterReadings != null) {
data['meterReadings'] =
this.meterReadings.map((v) => v.toJson()).toList();
}
data['belongingApartment'] = this.belongingApartment;
data['meterNumber'] = this.meterNumber;
data['meterType'] = this.meterType;
data['__v'] = this.iV;
return data;
}
}
class MeterReadings {
String sId;
String date;
int value;
MeterReadings({this.sId, this.date, this.value});
MeterReadings.fromJson(Map<String, dynamic> json) {
sId = json['_id'];
date = json['date'];
value = json['value'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['_id'] = this.sId;
data['date'] = this.date;
data['value'] = this.value;
return data;
}
}
class Meters {
String sId;
String dateStart;
String dateEnd;
int waterStart;
int waterEnd;
int powerStart;
int powerEnd;
Meters(
{this.sId,
this.dateStart,
this.dateEnd,
this.waterStart,
this.waterEnd,
this.powerStart,
this.powerEnd});
Meters.fromJson(Map<String, dynamic> json) {
sId = json['_id'];
dateStart = json['dateStart'];
dateEnd = json['dateEnd'];
waterStart = json['waterStart'];
waterEnd = json['waterEnd'];
powerStart = json['powerStart'];
powerEnd = json['powerEnd'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['_id'] = this.sId;
data['dateStart'] = this.dateStart;
data['dateEnd'] = this.dateEnd;
data['waterStart'] = this.waterStart;
data['waterEnd'] = this.waterEnd;
data['powerStart'] = this.powerStart;
data['powerEnd'] = this.powerEnd;
return data;
}
}

Not getting required value from json list - flutter

I have this code where am able to get values from an online json data and print its value
main.dart
final String url = 'https://raw.githubusercontent.com/BrightCode1/ohms-json/master/categories.json';
List<JsonModel> myModel = [];
List<CatSubcategory> subCate = [];
int localInt;
#override
void initState() {
// TODO: implement initState
super.initState();
localInt = 0;
loadData(localInt);
}
loadData(int dataInt) async {
var res = await http.get(url, headers: {"Accept": "application/json"});
if (res.statusCode == 200) {
String resBody = res.body;
var jsonDecode = json.decode(resBody);
for (var data in jsonDecode) {
data['cat_subcategory'].map((x) {
return subCate.add(
CatSubcategory(subName: x['sub_name'], subImage: x['sub_image']));
}).toList();
myModel.add(JsonModel(
category: data['category'],
catId: data['cat_id'],
catIcon: data['cat_icon'],
catSubcategory: subCate));
setState(() {});
}
print(myModel[dataInt].catSubcategory.length);
} else {
print("Something went wrong!");
}
}
my model.dart
class JsonModel {
JsonModel({
this.category,
this.catId,
this.catIcon,
this.catSubcategory,
});
String category;
String catId;
String catIcon;
List<CatSubcategory> catSubcategory;
factory JsonModel.fromJson(Map<String, dynamic> json) => JsonModel(
category: json["category"],
catId: json["cat_id"],
catIcon: json["cat_icon"],
catSubcategory: List<CatSubcategory>.from(
json["cat_subcategory"].map((x) => CatSubcategory.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"category": category,
"cat_id": catId,
"cat_icon": catIcon,
"cat_subcategory":
List<dynamic>.from(catSubcategory.map((x) => x.toJson())),
};
}
class CatSubcategory {
CatSubcategory({
this.subName,
this.subImage,
});
String subName;
String subImage;
factory CatSubcategory.fromJson(Map<String, dynamic> json) => CatSubcategory(
subName: json["sub_name"],
subImage: json["sub_image"],
);
Map<String, dynamic> toJson() => {
"sub_name": subName,
"sub_image": subImage,
};
}
The only problem am having now is that when I try to print the length on the subcategory of the first list it gives me 24 instead of 6 and i printed it like this myModel[dataInt].catSubcategory.length. The dataInt is 0 which means it's supposed to print out the length of the children("cat_subcategory") with category of Design & Creativity but it's printing all the other children of the other lis. So please how do i go about this. And if you need more explanation tell me
I would avoid writing your own model from scratch if you already know the schema of your json.
Just use something like https://app.quicktype.io/ just paste your json and you will get
import 'dart:convert';
class Root {
Root({
this.category,
this.catId,
this.catIcon,
this.catSubcategory,
});
String category;
String catId;
String catIcon;
List<CatSubcategory> catSubcategory;
factory Root.fromJson(Map<String, dynamic> json) => Root(
category: json["category"],
catId: json["cat_id"],
catIcon: json["cat_icon"] == null ? null : json["cat_icon"],
catSubcategory: List<CatSubcategory>.from(json["cat_subcategory"].map((x) => CatSubcategory.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"category": category,
"cat_id": catId,
"cat_icon": catIcon == null ? null : catIcon,
"cat_subcategory": List<dynamic>.from(catSubcategory.map((x) => x.toJson())),
};
}
class CatSubcategory {
CatSubcategory({
this.subName,
this.subImage,
});
String subName;
String subImage;
factory CatSubcategory.fromJson(Map<String, dynamic> json) => CatSubcategory(
subName: json["sub_name"],
subImage: json["sub_image"],
);
Map<String, dynamic> toJson() => {
"sub_name": subName,
"sub_image": subImage,
};
}
and then you should be good to just
loadData(int dataInt) async {
var res = await http.get(url, headers: {"Accept": "application/json"});
if (res.statusCode == 200) {
String resBody = res.body;
var list = json.decode(resBody) as List;
List<Root> items = list.map((i)=>Root.fromJson(i)).toList();
print(items[0].catSubcategory.length);
}
} else {
print("Something went wrong!");
}
}
https://repl.it/talk/share/Sample/118111

Why is my json code gives Instance of Post

I am trying to fetch data from internet. And this is the json string:
{"channel":{"id":1090161,"name":"İscaklik","description":"İscakliği okeyrum da.","latitude":"0.0","longitude":"0.0","field1":"nem","field2":"iscaklik","created_at":"2020-06-27T11:56:47Z","updated_at":"2020-06-27T11:56:47Z","last_entry_id":1},"feeds":[{"created_at":"2020-06-27T12:02:32Z","entry_id":1,"field1":"5"}]}
I want to get the "field1" in "feeds".
And my code is like that:
class _DataState extends State<Data> {
Future<String> AlbumState;
void initState() {
super.initState();
oku();
}
var a;
Future<List<Post>> oku() async {
final response = await http.get(
'https://api.thingspeak.com/channels/1090161/fields/1.json?api_key=JD9JYHAT2YHOI5Q3&results=1');
if (response.statusCode == 200) {
var responseJson = json.decode(response.body);
return (responseJson['feeds'] as List)
.map((p) => Post.fromJson(p))
.toList();
} else {
throw Exception('La bulamadık la böyle bi şey yoh la');
}
}
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Fetch Data Example',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
body: Center(
child: FutureBuilder<List<Post>>(
future: oku(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return Text(snapshot.data.toString());
} else if (snapshot.hasError) {
return Text("${snapshot.error}");
}
return CircularProgressIndicator();
},
),
),
),
);
}
}
class Post {
final String field1;
Post({this.field1});
factory Post.fromJson(Map<String, dynamic> json) {
return new Post(
field1: json['entry_id'].toString(),
);
}
}
This is the result:
I want to see the 5.(Which is the field1 of feeds)
Why I am seeing this?
How can I fix this?
Because you trying to use the entire object like the String in the Text widget. Try to call the attribute field1 you have in the Post class.
return Text(snapshot.data[0].field1);
Just Copy And Paste this model class and the simple use
if (response.statusCode == 200) {
var responseJson = json.decode(response.body);
ModelChannel channel = ModelChannel.fromJson(responseJson);
return channel.feeds;
} else {
throw Exception('La bulamadık la böyle bi şey yoh la');
}
Here is your JSON model
class ModelChannel {
Channel channel;
List<Feed> feeds;
ModelChannel({this.channel, this.feeds});
factory ModelChannel.fromJson(Map<String, dynamic> json) {
return ModelChannel(
channel: json['channel'] != null ? Channel.fromJson(json['channel']) : null,
feeds: json['feeds'] != null ? (json['feeds'] as List).map((i) => Feed.fromJson(i)).toList() : null,
);
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.channel != null) {
data['channel'] = this.channel.toJson();
}
if (this.feeds != null) {
data['feeds'] = this.feeds.map((v) => v.toJson()).toList();
}
return data;
}
}
class Feed {
String created_at;
int entry_id;
String field1;
Feed({this.created_at, this.entry_id, this.field1});
factory Feed.fromJson(Map<String, dynamic> json) {
return Feed(
created_at: json['created_at'],
entry_id: json['entry_id'],
field1: json['field1'],
);
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['created_at'] = this.created_at;
data['entry_id'] = this.entry_id;
data['field1'] = this.field1;
return data;
}
}
class Channel {
String created_at;
String description;
String field1;
String field2;
int id;
int last_entry_id;
String latitude;
String longitude;
String name;
String updated_at;
Channel({this.created_at, this.description, this.field1, this.field2, this.id, this.last_entry_id, this.latitude, this.longitude, this.name, this.updated_at});
factory Channel.fromJson(Map<String, dynamic> json) {
return Channel(
created_at: json['created_at'],
description: json['description'],
field1: json['field1'],
field2: json['field2'],
id: json['id'],
last_entry_id: json['last_entry_id'],
latitude: json['latitude'],
longitude: json['longitude'],
name: json['name'],
updated_at: json['updated_at'],
);
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['created_at'] = this.created_at;
data['description'] = this.description;
data['field1'] = this.field1;
data['field2'] = this.field2;
data['id'] = this.id;
data['last_entry_id'] = this.last_entry_id;
data['latitude'] = this.latitude;
data['longitude'] = this.longitude;
data['name'] = this.name;
data['updated_at'] = this.updated_at;
return data;
}
}
You may refer to this code, You should call the attribute data.feeds[0].field1-
function ItemListCtrl($scope, $http) {
$http.get("https://api.thingspeak.com/channels/1090161/fields/1.json?api_key=JD9JYHAT2YHOI5Q3&results=1", {
headers: {
'Content-Type': 'application/json'
}
}).success(function(data) {
console.log(data.feeds[0].field1)
$scope.data = angular.fromJson(data.feeds[0].field1);
});
}
Fiddle