I don't know if I'm making this way harder than it should be, but I'm not being able to see how can I track all of this. I'll explain:
I need to get a list of questions from an API request. This list can be of any length and, this is important, can be different kind of answers. More specifically, there are nine different questions, each of one requires a different answer: checkbox, boolean, integer, selectable integers, float, single selection, multiple selection, short text and long text. So far so good, this is easy to get and display.
My problem is: how can I track the answers to those questions? I need a final "send" button that recolects (as a list) the different answers from those different widgets (that not all are, say, forms, as there are things like checkboxs) to make a post request to the API containing those answers. As I said, I've no problem displaying them, but I don't get around how to recolect unknown answers from an unknown quantity of unknown widgets.
Just to see a possible example:
With this future:
Future<List<FeedbackQuestion>?> _getFeedbackQuestion() async {
Map<String, String> requestHeaders = {
'Content-type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Token $apiToken'
};
final response =
await http.get(Uri.parse("$feedbackQuestionsURL"), headers: requestHeaders);
if (response.statusCode == 200) {
List<FeedbackQuestion> feedbackQuestions = (json.decode(response.body) as List)
.map((data) => FeedbackQuestion.fromJson(data))
.toList();
setState(() {
isLoading = false;
});
return feedbackQuestions;
}
}
It is easy to display different widgets depending on the type of questions. For example, in a ListView.builder could be something like this:
FutureBuilder<List<FeedbackQuestion>?>(
future: _getFeedbackQuestion(),
builder: (context, snapshot) {
return snapshot.hasData
? ListView.builder(
itemCount: snapshot.data!.length,
itemBuilder: (BuildContext context, int index) {
return Container(child: Center(child:
snapshot.data![index].answer_type! == "bc" ? BooleanCheckboxQuestion()
: snapshot.data![index].answer_type! == "bs" ? BooleanSelectionQuestion()
: snapshot.data![index].answer_type! == "i" ? IntegerQuestion()
: snapshot.data![index].answer_type! == "is" ? IntegerSelectionQuestion()
: snapshot.data![index].answer_type! == "f" ? FloatQuestion()
: snapshot.data![index].answer_type! == "ss" ? SingleSelectionQuestion()
: snapshot.data![index].answer_type! == "ms" ? MultipleSelectionQuestion()
: snapshot.data![index].answer_type! == "t" ? TextQuestion()
: LongTextQuestion()
,),
);
})
But the question is... how can I track the answers to those questions in order to list them and send it as a POST request for the API?
Thank you very much!
I don't know how your POST request should work, but each widget you have could receive a callback to add their answer.
class Answer<T> {
Answer({
required this.id,
required this.value,
});
final String id;
final T value;
}
Your page could have the list of answers
final List<Answer> answers = [];
void updateList(Answer answer) {
final find = answers.where((e) => e.id == answer.id).isNotEmpty();
if (find) {
answers[answers.indexWhere((e) => e.id == answer.id)] = answer;
} else {
answers.add(answer);
}
}
And each of your widgets could receive a
void Function(Answer)
to update the list of answers with the value each one choose
Now you have a list of answers, I don't know if each of your questions has an id, or it's identified by order, so you would have to change a little. But now you can use this list to make your SEND request.