显示“ class_name的实例”而不是数据

安奇

我正在尝试从API获取数据,但是当执行以下代码时,我仅作为“类Survey的实例”获得响应,而没有获得API中的实际数据。这是我正在使用的代码:

Future GetSurvey() async {
    var response = await http.get(
      url,
      headers: {
        "Accept": "application/json"
      }
    );
if (response.statusCode == 200) {
      // If the server did return a 200 OK response,
      // then parse the JSON.
      var data = convertoJson['Item'];
      print(Questions.fromjson(data));*/

      Map<String, dynamic> map = json.decode(response.body);
      var data = map['Item'];
      Survey survey = new Survey.fromjson(data);
      print(survey.questions);


    } else {
      // If the server did not return a 200 OK response,
      // then throw an exception.
      throw Exception('Failed to load album');
    }



  }

}

class Questions{
  String questions;

  Questions({this.questions});

  factory Questions.fromjson(Map<String, dynamic> json){


    //var questionfromjson = json['question'];
    //List<String> questionslist = new List<String>.from(questionfromjson);

    return Questions(
      questions: json['question']
    );
  }

}

class Survey{
  String surveyid;
  List<Questions> questions;

  Survey({this.surveyid,this.questions});

  factory Survey.fromjson(Map<String, dynamic> json){

    var list = json['QUESTIONS'] as List;
    print(list.runtimeType);

    List<Questions> questionslist = list.map((e) => Questions.fromjson(e)).toList();

    return Survey(
      surveyid: json['OS_ID'],
      questions: questionslist
    );
  }

}

以下是API的格式:

{
    "Item":
    {
        "OS_ID": "759",
        "QUESTIONS":[
        {
            "rule":,
            "question": "How much is the value"
        },
        {
            "rule":,
            "question": "Describe the view"
        }
    }
}
鞭打

不用担心,您的代码很好。只需toString()在模型类中重写函数即可。

@override
  String toString() {
    return 'questions{questions: $questions}';
  }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章