updateItems method Null safety

  1. @override
Future<List<Map<String, String>>> updateItems(
  1. GlpiItemType itemType,
  2. String data
)
override

Return the id(s) of the item(s) updated using the data as input. To correctly format the data, see the examples and the documentation. Reference: https://github.com/glpi-project/glpi/blob/master/apirest.md#update-items

Implementation

@override
Future<List<Map<String, String>>> updateItems(
    GlpiItemType itemType, String data) async {
  if (sessionToken!.isEmpty) {
    throw Exception('No session token, initSession first');
  }

  final Map<String, String> headers = {
    'Session-Token': sessionToken!,
    'Content-Type': 'application/json',
    ...?appToken != null ? {'App-Token': appToken!} : null,
  };

  final uri = Uri.parse('$host/${itemType.toString().split('.').last}');

  final response = await _innerClient.put(uri, headers: headers, body: data);

  if (response.statusCode != 200 && response.statusCode != 207) {
    throw GlpiException.fromResponse(
        response.statusCode, json.decode(response.body));
  }

  List<dynamic> decodedJson = json.decode(response.body);

  List<Map<String, String>> formatted = decodedJson.map((element) {
    return element as Map<String, String>;
  }).toList();

  return Future.value(formatted);
}