getBlocks method

Future<MBPaginatedResponse<MBBlock>> getBlocks({
  1. List<MBParameter> parameters = const [],
  2. bool includeSections = false,
  3. bool includeElements = false,
})

Retrieve the blocks of the project.

  • Parameters:
    • parameters: An optional array of MBParameter used to sort, an empty array by default.
    • includeSections: If true the sections of the block are included in the response, false by default.
    • includeElements: If true 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 MBBlock: the blocks retrieved and information about pagination.

Implementation

Future<MBPaginatedResponse<MBBlock>> getBlocks({
  List<MBParameter> parameters = const [],
  bool includeSections = false,
  bool includeElements = false,
}) async {
  Map<String, String> apiParameters = {};

  if (includeSections) {
    apiParameters['include'] =
        includeElements ? 'sections.elements' : 'sections';
  }

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

  String apiName = 'api/blocks/';

  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<MBBlock>(
    from: meta['from'] as int,
    to: meta['to'] as int,
    total: meta['total'] as int,
    items: items.map((item) => MBBlock.fromDictionary(item)).toList(),
  );
}