uploadAvatar method

Future<void> uploadAvatar(
  1. dynamic context,
  2. dynamic ref
)

Implementation

Future<void> uploadAvatar(BuildContext context, WidgetRef ref) async {
  try {
    final account = ref.watch(accountProvider);
    if (selectedUserAvatar.value == null ||
        selectedUserAvatar.value?.path == null) {
      if (context.mounted) {
        EasyLoading.showToast(L10n.of(context).avatarEmpty);
      }
      return;
    }
    if (context.mounted) {
      EasyLoading.show(status: L10n.of(context).avatarUploading);
    }
    await account.uploadAvatar(selectedUserAvatar.value!.path!);
    ref.invalidate(accountProvider);
    EasyLoading.dismiss(); // close loading
    if (context.mounted) context.goNamed(Routes.main.name);
  } catch (e, s) {
    _log.severe('Failed to upload avatar', e, s);
    if (!context.mounted) {
      EasyLoading.dismiss();
      return;
    }
    EasyLoading.showError(
      L10n.of(context).avatarUploadFailed(e),
      duration: const Duration(seconds: 3),
    );
  }
}