2021年1月18日星期一

Rest API Consumption in flutter

Problem

I'm trying to receive data from a rest API in my flutter app, inside a future function but i keep getting the following error: type 'CastList<dynamic, List>' is not a subtype of type 'List'.

The function I'm using to fetch the data is as follows:

static Future<List<Questionnaire>> fetchQuestionnaires() async {      try {        final response =            await http.get('http://10.0.2.2:4010/phone/questionnaires');        if (response.statusCode == 200) {            List<Questionnaire> resp =              json.decode(response.body).cast<List<Questionnaire>>();            List<Questionnaire> questionnaires = resp              .map<Questionnaire>((dynamic item) => Questionnaire.fromJson(item))              .toList();            log(            questionnaires.toString(),          );            return questionnaires;        } else {          throw Exception('Unable to fetch questionnaires');        }      } catch (error) {        log(error.toString());      }    }  

I don't understand why this is. Why does using cast, cast to CastList<dynamic, Type> and not the original Type? What changes do I do to get my data?

The data model is given below.

Data Model

The Data i expect from my backend is like this. An array called questionnaires, containing multiples of a questionnaire, each containing an id and a displayQuestion. A displayQuestion in turn has the question text and the answers Array_.

For this, I have the following structure in my Json.

[      {          "questionId": 1,          "displayQuestions": [              {                  "question": "how old are you",                  "answers": [                      "1",                      "2",                      "3",                      "4"                  ]              }          ]      }  ]  

This is my questionnaires.dart model

class Questionnaires {    List<Questionnaire> _questionnaires;    Questionnaires.fromJson(this._questionnaires);  }  

This is questionnaire.dart model

class Questionnaire {    int questionnaireId;    List<DisplayQuestion> displayQuestions = [];    Questionnaire(this.questionnaireId, this.displayQuestions);    Questionnaire.fromJson(Map<String, dynamic> json)        : questionnaireId = json['questionnaireId'],          displayQuestions = json['displayQuestions'];  }  

this is display-question.dart model

class Questionnaire {    int questionnaireId;    List<DisplayQuestion> displayQuestions = [];    Questionnaire(this.questionnaireId, this.displayQuestions);    Questionnaire.fromJson(Map<String, dynamic> json)        : questionnaireId = json['questionnaireId'],          displayQuestions = json['displayQuestions'];  }  

EDIT

The code from display-question.dart model

class DisplayQuestion {    String _question;    List<String> _answers;    String get question => this._question;      List get answers => this._answers;      DisplayQuestion(this._question, [List<String> answers])        : this._answers = answers ?? [];      DisplayQuestion.fromJson(Map<String, dynamic> json)        : _question = json['question'],          _answers = json['answers'];  }  
https://stackoverflow.com/questions/65783405/rest-api-consumption-in-flutter January 19, 2021 at 07:17AM

没有评论:

发表评论