简体   繁体   中英

Flutter Dart Parsing Json Strings into Objects

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);

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM