Flutter中解析的json中有多个对象

时间:2018-08-23 07:56:04

标签: dart flutter

我正在使用的模型:

 class ExerciseSession{
      final String Id;
      final String cw_id;
      final String client_id;
      final String date;
      final int challenge_rating;
      final String apiCode;
      final String userId;

      ExerciseSession({this.Id, this.cw_id, this.client_id, this.date,
        this.challenge_rating, this.apiCode, this.userId});

      factory ExerciseSession.fromJSON(Map<String, dynamic> json){
        return ExerciseSession(
            Id: json['session_id'],
            cw_id: json['cw_id'],
            client_id: json['client_id'],
            date: json['session_date'],
            challenge_rating: json['challenge_rating'],
        );
      }

      Map<String, String> toBodyMap()=>{
      "cwId" : this.cw_id,
      "clientid" : this.client_id,
      "sessionDate" : this.date,
      "challengeRating" : this.challenge_rating.toString(),
        "apiCode" : this.apiCode,
        "userId" : this.userId,
      };
    }

    class ExerciseList {
      final List<ExerciseSession> sessions;

      ExerciseList({this.sessions});

      factory ExerciseList.fromJSON(List<dynamic> json){

        List<ExerciseSession> sessions = new List<ExerciseSession>();
        sessions = json.map((i)=>ExerciseSession.fromJSON(i)).toList();

        return new ExerciseList(
            sessions: sessions
        );
      }
    }

我用来从服务器获取json的代码:

Future<ExerciseList> getExercises(String userId, String clientId,
      {DateTime singleDate, DateTime fromDate, DateTime toDate}) async {
    print("Future Get Exercises");
    String _userId = userId;
    String _clientId = clientId;

    ExerciseList sessions;

    if(singleDate != null){
      String _onDate = singleDate.toIso8601String();
    print("Single Date Set");
    http.post(phpUrl, body: {'userId':_userId, 'clientid':_clientId, 'onDate':_onDate, 'apiCode':apc.getExercisesFromOneDate_AC}).then((response){
      print("Response status: ${response.statusCode}");
      print("Response body: ${response.body}");
      if(response.statusCode == 200){
        sessions = ExerciseList.fromJSON(json.decode(response.body));
      } else {
        throw Exception("Failed to Get Assessments");
      }
    });
  } else if(fromDate != null && toDate != null){
      String _fromDate = fromDate.toIso8601String();
      String _toDate = toDate.toIso8601String();
    print("From to Date Set");
    http.post(phpUrl, body: {'userId':_userId, 'clientid':_clientId, 'fromDate':_fromDate, 'toDate':_toDate, 'apiCode':apc.getExercisesDateRange_AC}).then((response){
      print("Response status: ${response.statusCode}");
      print("Response body: ${response.body}");
      if(response.statusCode == 200){
        sessions = ExerciseList.fromJSON(json.decode(response.body));
      } else {
        throw Exception("Failed to Get Assessments");
      }
    });
  } else if(fromDate != null){
      String _fromDate = fromDate.toIso8601String();
    print("From Date Set");
    http.post(phpUrl, body: {'userId':_userId, 'clientid':_clientId, 'fromDate':_fromDate, 'apiCode':apc.getExercisesFromDate_AC}).then((response){
      print("Response status: ${response.statusCode}");
      print("Response body: ${response.body}");
      if(response.statusCode == 200){
        sessions = ExerciseList.fromJSON(json.decode(response.body));
      } else {
        throw Exception("Failed to Get Assessments");
      }
    });
  } else {
    print("No Dates Set");
    http.post(phpUrl, body: {'userId':_userId, 'clientid':_clientId, 'apiCode':apc.getExercises_AC}).then((response){
      print("Response status: ${response.statusCode}");
      print("Response body: ${response.body}");
      if(response.statusCode == 200){
        sessions = ExerciseList.fromJSON(json.decode(response.body));
      } else {
        throw Exception("Failed to Get Assessments");
      }
    });
  }

    return sessions;
  }

我的测试响应代码:

void _getAllExercises() async{
    print("[$TAG : INFO] - Getting Exercises");
    ExerciseList getList = await exerRest.getExercises("1", "2");
    ExerciseSession session = getList.sessions[0];
    print("[TAG : INFO] - Exercise Session Test All challenge rating ${session.challenge_rating}");
  }

和控制台输出:

I / flutter(8157):[调试屏幕:信息]-开始锻炼 I / flutter(8157):未来锻炼 I / flutter(8157):未设置日期 I / flutter(8157):响应状态:200 I / flutter(8157):响应正文:[{“ session_id”:“ 10”,“ cw_id”:“ 1”,“ client_id”:“ 2”,“ session_date”:“ 2018-08-23”,“ challenge_rating “:” 0“},{” session_id“:” 15“,” cw_id“:” 1“,” client_id“:” 2“,” session_date“:” 2018-08-23“,” challenge_rating“:” 1 “},{” session_id“:” 17“,” cw_id“:” 1“,” client_id“:” 2“,” session_date“:” 2018-08-23“,” challenge_rating“:” 2“}]] < / p>

我不知道我在做什么错,看起来应该可以正常工作,但显然我缺少了一些东西,

我怀疑这是我的模型.fromJson和json本身之间的混淆。

0 个答案:

没有答案