Displaying 'Instance of class_name' instead of data


I am trying to get the data from an API, but when executing the following the code I am getting the response as only ‘Instance of class Survey’ but not the actual data in the API. Here is the code I am using:

Future GetSurvey() async {
    var response = await http.get(
      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'];

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

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


  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;


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

    var list = json['QUESTIONS'] as List;

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

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


Following is the format of the API:

        "OS_ID": "759",
            "question": "How much is the value"
            "question": "Describe the view"


Nothing to worry, your code is fine. Just need to override toString() function inside your model class..

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

Answered By – towhid

Answer Checked By – David Goodson (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *