getAvatar method

Future<Uint8List> getAvatar([
  1. String? filename
])

Downloads a user's avatar from NTUT Portal.

If filename is omitted or empty, the server returns a dynamically generated placeholder avatar (a colored square with the user's name).

Returns the avatar image as raw bytes.

Implementation

Future<Uint8List> getAvatar([String? filename]) async {
  final response = await _portalDio.get(
    'photoView.do',
    queryParameters: {'realname': filename ?? ''},
    options: Options(responseType: ResponseType.bytes),
  );

  final contentType = response.headers.value('content-type') ?? '';
  final mediaType = MediaType.parse(contentType);
  if (mediaType.type != 'image') {
    throw FormatException(
      'Expected image response, got Content-Type: $contentType',
    );
  }

  return response.data;
}