uploadAvatar method

Future<void> uploadAvatar(
  1. Uint8List bytes
)

Uploads bytes as the user's new avatar.

The server stores the image and stamps the new avatarUrl on the profile itself, so this re-reads rather than constructing a URL locally.

Implementation

Future<void> uploadAvatar(Uint8List bytes) async {
  final current = state.value;
  if (current == null) return;

  // Evict the old image before uploading. The new URL carries a fresh
  // cache-buster so the old entry would never be requested again anyway;
  // evicting reclaims disk and memory now instead of at LRU expiry.
  final oldUrl = resolveAvatarUrl(
    current.avatarUrl,
    ref.read(appConfigProvider).engine.apiBaseUrl,
  );
  if (oldUrl != null) await CachedNetworkImageProvider(oldUrl).evict();

  state = const AsyncLoading<Profile>();
  try {
    await ref.read(avatarStorageServiceProvider).uploadAvatar(bytes);
    await _reload(current);
  } catch (_) {
    await _restore(current);
    rethrow;
  }
}