Files
eatvio_app/lib/features/menu/presentation/notification_settings_user_message.dart

43 lines
1.2 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import 'package:dio/dio.dart';
const notificationSettingsSaveFailedMessage =
'Не удалось сохранить настройки уведомлений';
String notificationSettingsUpdateAlertMessage(Object error) {
final validationMessage = _validationMessage(error);
if (validationMessage != null) return validationMessage;
return notificationSettingsSaveFailedMessage;
}
String? _validationMessage(Object error) {
if (error is! DioException || error.response?.statusCode != 422) return null;
final data = error.response?.data;
if (data is! Map) return null;
final errors = data['errors'];
if (errors is Map) {
for (final value in errors.values) {
final text = _firstErrorText(value);
if (text != null) return text;
}
}
final message = data['message'];
if (message is String && message.trim().isNotEmpty) return message.trim();
return null;
}
String? _firstErrorText(Object? value) {
if (value is List) {
for (final item in value) {
final text = item.toString().trim();
if (text.isNotEmpty) return text;
}
return null;
}
if (value is String && value.trim().isNotEmpty) return value.trim();
return null;
}