votePoll method Null safety

Future<MBPollVoteResponse> votePoll(
  1. int pollId,
  2. int answerIndex
)

Votes for a poll.

  • Parameters:
    • pollId: the id of the poll element.
    • answerIndex: this index of the answer voted for.
  • Returns a Future that completes with the MBPollVoteResponse posted to MBurger.

Implementation

Future<MBPollVoteResponse> votePoll(int pollId, int answerIndex) async {
  Map<String, dynamic> apiParameters = {};

  apiParameters['element_id'] = pollId.toString();
  apiParameters['vote'] = answerIndex.toString();

  apiParameters.addAll(await MBManager.shared.defaultParameters());

  String apiName = 'api/vote-poll';

  var requestBody = json.encode(apiParameters);

  Map<String, String> headers =
      await MBManager.shared.headers(contentTypeJson: true);

  var uri = Uri.https(MBManager.shared.endpoint, apiName);

  http.Response response = await http.post(
    uri,
    headers: headers,
    body: requestBody,
  );

  Map<String, dynamic> body = MBManager.checkResponse(response.body);
  return MBPollVoteResponse(dictionary: body);
}