forked from eatvio/eatvio_app
feat(profile): some fixes, add buttons
This commit is contained in:
72
lib/old/users/subscribers_screen/bloc/subscribers_bloc.dart
Normal file
72
lib/old/users/subscribers_screen/bloc/subscribers_bloc.dart
Normal file
@@ -0,0 +1,72 @@
|
||||
//import 'package:eatvio_app/old/auth/auth_screen/auth_screen.dart';
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:eatvio_app/app_router.dart';
|
||||
import 'package:eatvio_app/core/token_storage.dart';
|
||||
import 'package:eatvio_app/old/users/subscribers_screen/models/subscribers_model.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
import '/core/app_export.dart';
|
||||
|
||||
part 'subscribers_event.dart';
|
||||
part 'subscribers_state.dart';
|
||||
|
||||
/// A bloc that manages the state of a Subscribers according to the event that is dispatched to it.
|
||||
class SubscribersBloc extends Bloc<SubscribersEvent, SubscribersState> {
|
||||
SubscribersBloc(SubscribersState initialState) : super(initialState) {
|
||||
on<SubscribersInitialEvent>(_onInitialize);
|
||||
}
|
||||
|
||||
Future<void> _onInitialize(
|
||||
SubscribersInitialEvent event,
|
||||
Emitter<SubscribersState> emit,
|
||||
) async {
|
||||
try {
|
||||
if (!await TokenStorage.haveTokenInStorage()) {
|
||||
AppRouter.navigatorKey.currentContext?.goNamed('authScreen');
|
||||
}
|
||||
final response = await http.get(
|
||||
Uri.parse(baseUrl + '/api/profile/subscribers'),
|
||||
headers: {
|
||||
'Authorization': 'Bearer ${await TokenStorage.getTokenFromStorage()}',
|
||||
},
|
||||
);
|
||||
print('Got response');
|
||||
print(response.statusCode);
|
||||
print(response.body);
|
||||
if (response.statusCode == 200) {
|
||||
print('Got 200');
|
||||
final data = jsonDecode(response.body);
|
||||
if (data is List<dynamic>) {
|
||||
print('Got List');
|
||||
if (state is SubscribersLoadedState) {
|
||||
print('Got loaded');
|
||||
var currentState = state as SubscribersLoadedState;
|
||||
emit((currentState).copyWith(
|
||||
subscribers: data,
|
||||
myProfileModelObj: currentState.myProfileModelObj,
|
||||
));
|
||||
} else {
|
||||
print('Got unloaded');
|
||||
emit(SubscribersLoadedState(
|
||||
subscribers: data,
|
||||
));
|
||||
}
|
||||
} else {
|
||||
// Один или несколько ключевых полей отсутствуют, обрабатываем ошибку
|
||||
emit(SubscribersErrorState('Неверный формат ответа API.'));
|
||||
}
|
||||
} else {
|
||||
// Статус ответа не равен 200, обрабатываем ошибку
|
||||
emit(SubscribersErrorState('Ошибка сервера: ${response.statusCode}'));
|
||||
}
|
||||
} catch (e) {
|
||||
// Ошибка при декодировании JSON или другая ошибка выполнения
|
||||
emit(SubscribersErrorState('Ошибка при получении данных ленты: $e'));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user