Reputation: 41
Sup! So my problem is, my Dashboard Model isnt getting data assigned from the json string parsed.Basically Dashboard.userActivity and Dashboard.appName are NULL when i print into console. I cant realy get behind why. the testDataFunction should print the corresponding Dashboard Object with all their data (2 nested classes Performance and UserActivity and 3 variables errors, appname,time. I did not include the code of Perf and UserAct. as its the same as Dashboard with toJson and fromJson helper Methods.
What did i oversaw? Much thanks in advance!
import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
class Dashboard {
UserActivity? userActivity; ///class
int? totalError;
Performance? performance; ///class
String? appName;
String? time;
Dashboard(
{this.userActivity,
this.totalError,
this.performance,
this.appName,
this.time});
Dashboard.fromJson(Map<dynamic, dynamic> json) {
userActivity = json['userActivity'] != null
? new UserActivity.fromJson(json['userActivity'])
: null;
totalError = json['totalError'];
performance = json['performance'] != null
? new Performance.fromJson(json['performance'])
: null;
appName = json['appName'];
time = json['time'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.userActivity != null) {
data['userActivity'] = this.userActivity!.toJson();
}
data['totalError'] = this.totalError;
if (this.performance != null) {
data['performance'] = this.performance!.toJson();
}
data['appName'] = this.appName;
data['time'] = this.time;
return data;
}
@override
toString() {
return "userActivity: " + userActivity.toString() + ", appName: " + appName!;
}
}
Future testDataFunktion() async {
String backendjsondata = '{"dashboard":{"userActivity":{"total":17,"logins":50,"active":1,"inactive":5,"loginsPerHourLst":[0,0,0,0,0,0,0,4,11,13,3,3,0,0,10,6],"hourLst":["0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15"],"online":6},"totalError":1,"performance":{"loadTimePagesLst":[583,289,154,105,16,13,4,583,0,0],"totalPageViewsPerHourLst":[0,0,0,0,0,0,0,14,82,104,52,85,9,89,114,34],"hourLst":["0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15"],"totalPageViews":583},"appName":"intranet","time":"January, 24 2022 15:50:48"}}';
Map<String, dynamic> data = jsonDecode(backendjsondata);
Dashboard dashboard = Dashboard.fromJson(data);
print(dashboard);
Upvotes: 0
Views: 124
Reputation: 12713
Try:
Future testDataFunktion() async {
String backendjsondata = '{"dashboard":{"userActivity":{"total":17,"logins":50,"active":1,"inactive":5,"loginsPerHourLst":[0,0,0,0,0,0,0,4,11,13,3,3,0,0,10,6],"hourLst":["0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15"],"online":6},"totalError":1,"performance":{"loadTimePagesLst":[583,289,154,105,16,13,4,583,0,0],"totalPageViewsPerHourLst":[0,0,0,0,0,0,0,14,82,104,52,85,9,89,114,34],"hourLst":["0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15"],"totalPageViews":583},"appName":"intranet","time":"January, 24 2022 15:50:48"}}';
Dashboard dashboard = Dashboard.fromJson(Map.from(jsonDecode(backendjsondata)));
print(dashboard);
}
Upvotes: 1