forked from eatvio/eatvio_app
Error state added. Error display added for profile. Carousel in a separate function
This commit is contained in:
@@ -17,9 +17,10 @@ class MyProfileBloc extends Bloc<MyProfileEvent, MyProfileState> {
|
||||
}
|
||||
|
||||
Future<void> _onInitialize(
|
||||
MyProfileInitialEvent event,
|
||||
Emitter<MyProfileState> emit,
|
||||
) async {
|
||||
MyProfileInitialEvent event,
|
||||
Emitter<MyProfileState> emit,
|
||||
) async {
|
||||
try {
|
||||
final box = await Hive.openBox('token');
|
||||
final token = box.get('token');
|
||||
final response = await http.get(
|
||||
@@ -28,36 +29,58 @@ class MyProfileBloc extends Bloc<MyProfileEvent, MyProfileState> {
|
||||
'Authorization': 'Bearer $token',
|
||||
},
|
||||
);
|
||||
final data = jsonDecode(response.body);
|
||||
|
||||
if (state is MyProfileLoadedState) {
|
||||
// Если текущее состояние уже загружено, обновляем его
|
||||
emit((state as MyProfileLoadedState).copyWith(
|
||||
user: data['user'],
|
||||
userDetails: data['userDetails'],
|
||||
token: token,
|
||||
chartPoints: data['chartPoints'],
|
||||
plans: data['plans'],
|
||||
chestCircumferenceController: TextEditingController(),
|
||||
waistCircumferenceController: TextEditingController(),
|
||||
hipCircumferenceController: TextEditingController(),
|
||||
weightController: TextEditingController(),
|
||||
circumferences: data['circumferences'],
|
||||
));
|
||||
if (response.statusCode == 200) {
|
||||
final data = jsonDecode(response.body);
|
||||
|
||||
// Проверка наличия ключевых полей в JSON
|
||||
if (data is Map<String, dynamic> &&
|
||||
data.containsKey('user') &&
|
||||
data.containsKey('userDetails') &&
|
||||
data.containsKey('chartPoints') &&
|
||||
data.containsKey('plans') &&
|
||||
data.containsKey('circumferences')) {
|
||||
// Все ключевые поля присутствуют, продолжаем обработку
|
||||
if (state is MyProfileLoadedState) {
|
||||
// Если текущее состояние уже загружено, обновляем его
|
||||
emit((state as MyProfileLoadedState).copyWith(
|
||||
user: data['user'],
|
||||
userDetails: data['userDetails'],
|
||||
token: token,
|
||||
chartPoints: data['chartPoints'],
|
||||
plans: data['plans'],
|
||||
chestCircumferenceController: TextEditingController(),
|
||||
waistCircumferenceController: TextEditingController(),
|
||||
hipCircumferenceController: TextEditingController(),
|
||||
weightController: TextEditingController(),
|
||||
circumferences: data['circumferences'],
|
||||
));
|
||||
} else {
|
||||
// Если текущее состояние - начальное, создаем новое загруженное состояние
|
||||
emit(MyProfileLoadedState(
|
||||
user: data['user'],
|
||||
userDetails: data['userDetails'],
|
||||
token: token,
|
||||
chartPoints: data['chartPoints'],
|
||||
plans: data['plans'],
|
||||
chestCircumferenceController: TextEditingController(),
|
||||
waistCircumferenceController: TextEditingController(),
|
||||
hipCircumferenceController: TextEditingController(),
|
||||
weightController: TextEditingController(),
|
||||
circumferences: data['circumferences'],
|
||||
));
|
||||
}
|
||||
} else {
|
||||
// Один или несколько ключевых полей отсутствуют, обрабатываем ошибку
|
||||
emit(MyProfileErrorState('Некоторые необходимые данные отсутствуют.'));
|
||||
}
|
||||
} else {
|
||||
// Если текущее состояние - начальное, создаем новое загруженное состояние
|
||||
emit(MyProfileLoadedState(
|
||||
user: data['user'],
|
||||
userDetails: data['userDetails'],
|
||||
token: token,
|
||||
chartPoints: data['chartPoints'],
|
||||
plans: data['plans'],
|
||||
chestCircumferenceController: TextEditingController(),
|
||||
waistCircumferenceController: TextEditingController(),
|
||||
hipCircumferenceController: TextEditingController(),
|
||||
weightController: TextEditingController(),
|
||||
circumferences: data['circumferences'],
|
||||
));
|
||||
// Статус ответа не равен 200, обрабатываем ошибку
|
||||
emit(MyProfileErrorState('Ошибка сервера: ${response.statusCode}'));
|
||||
}
|
||||
} catch (e) {
|
||||
// Ошибка при декодировании JSON или другая ошибка выполнения
|
||||
emit(MyProfileErrorState('Ошибка при получении данных профиля: $e'));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -85,3 +85,11 @@ class MyProfileLoadedState extends MyProfileState {
|
||||
}
|
||||
}
|
||||
|
||||
class MyProfileErrorState extends MyProfileState {
|
||||
final String errorMessage;
|
||||
|
||||
const MyProfileErrorState(this.errorMessage);
|
||||
|
||||
@override
|
||||
List<Object> get props => [errorMessage];
|
||||
}
|
||||
Reference in New Issue
Block a user