getSections method

Future<MBPaginatedResponse<MBSection>> getSections({
  1. required int blockId,
  2. List<MBParameter> parameters = const [],
  3. bool includeElements = false,
})

Retrieve the sections of the block with the specified id.

  • Parameters:
    • blockId: The blockId of the block.
    • parameters: An optional array of MBParameter used to sort, an empty array by default.
    • includeElements: If true of the elements in the sections of the blocks are included in the response, false by default.
  • Returns a Future that completes with a paginated response of MBSection: the sections retrieved and information about pagination.

Implementation

Future<MBPaginatedResponse<MBSection>> getSections({
  required int blockId,
  List<MBParameter> parameters = const [],
  bool includeElements = false,
}) async {
  Map<String, String> apiParameters = {};

  if (includeElements) {
    apiParameters['include'] = 'elements';
  }

  for (MBParameter parameter in parameters) {
    Map<String, String> representation = parameter.representation;
    if (representation.isNotEmpty) {
      apiParameters.addAll(representation);
    }
  }

  String apiName = 'api/blocks/$blockId/sections';

  apiParameters.addAll(await defaultParameters());

  var uri = Uri.https(endpoint, apiName, apiParameters);
  var response = await http.get(uri, headers: await headers());
  Map<String, dynamic> body = MBManager.checkResponse(response.body);
  Map<String, dynamic> meta = body['meta'] as Map<String, dynamic>;
  List<Map<String, dynamic>> items =
      List<Map<String, dynamic>>.from(body['items'] as List);
  return MBPaginatedResponse<MBSection>(
    from: meta['from'] as int,
    to: meta['to'] as int,
    total: meta['total'] as int,
    items: items.map((item) => MBSection.fromDictionary(item)).toList(),
  );
}