MBPollElement constructor
Initializes a poll element with the dictionary returned by the MBurger APIs.
- Parameters:
dictionary
: Thedictionary
returned by the APIs.
Implementation
factory MBPollElement({required Map<String, dynamic> dictionary}) {
List<MBPollAnswer> answers = [];
bool answered = false;
MBPollAnswer? answer;
DateTime? expiration;
if (dictionary['value'] is Map<String, dynamic>) {
Map<String, dynamic> value = dictionary['value'] as Map<String, dynamic>;
List<dynamic> answersFromApi = value['answers'] as List;
List<dynamic> resultsFromApi = value['results'] as List;
int index = 0;
for (var answer in answersFromApi) {
if (answer != null &&
answer is String &&
index < resultsFromApi.length) {
dynamic votesFromDict = resultsFromApi[index];
int votes = 0;
if (votesFromDict is int) {
votes = votesFromDict;
} else if (votesFromDict is double) {
votes = votesFromDict.toInt();
}
answers.add(MBPollAnswer(answer, votes));
}
index++;
}
if (value['answered'] is bool) {
answered = value['answered'] as bool;
}
if (value['answer'] is int) {
int answerInt = value['answer'] as int;
if (answerInt < answers.length) {
answer = answers[answerInt];
}
}
if (value['ends_at'] is int) {
int endsAtTimestamp = value['ends_at'] as int;
expiration =
DateTime.fromMillisecondsSinceEpoch(endsAtTimestamp * 1000);
}
}
return MBPollElement._(
dictionary: dictionary,
answers: answers,
answered: answered,
answer: answer,
expiration: expiration,
);
}