mirror of
https://github.com/smmarty/friflex_flutter_starter.git
synced 2026-02-05 19:52:17 +00:00
feat(app): Добавить пример со Scope (#5)
* feat(app): Добавить пример со Scope * fix scope * feat: добавить скоуп с внутренней зависимостью от репозитория (#6) Co-authored-by: Artem Barkalov <artembark@gmail.com> * feat: исправить обалсть видимости ProfileScope * feat: добавить фикс namespace плагинов --------- Co-authored-by: PetrovY <y.petrov@friflex.com> Co-authored-by: Artem Barkalov <artembark@gmail.com>
This commit is contained in:
39
lib/features/profile/domain/bloc/profile_bloc.dart
Normal file
39
lib/features/profile/domain/bloc/profile_bloc.dart
Normal file
@@ -0,0 +1,39 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:friflex_starter/features/profile/domain/repository/i_profile_repository.dart';
|
||||
|
||||
part 'profile_event.dart';
|
||||
part 'profile_state.dart';
|
||||
|
||||
class ProfileBloc extends Bloc<ProfileEvent, ProfileState> {
|
||||
ProfileBloc(this._profileRepository) : super(ProfileInitialState()) {
|
||||
// Вам необходимо добавлять только
|
||||
// один обработчик событий в конструкторе
|
||||
on<ProfileEvent>((event, emit) async {
|
||||
if (event is ProfileFetchProfileEvent) {
|
||||
await _fetchProfile(event, emit);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
final IProfileRepository _profileRepository;
|
||||
|
||||
Future<void> _fetchProfile(
|
||||
ProfileFetchProfileEvent event,
|
||||
Emitter<ProfileState> emit,
|
||||
) async {
|
||||
try {
|
||||
emit(ProfileWaitingState());
|
||||
final data = await _profileRepository.fetchUserProfile(event.id);
|
||||
emit(ProfileSuccessState(data: data));
|
||||
} on Object catch (error, stackTrace) {
|
||||
emit(
|
||||
ProfileErrorState(
|
||||
message: 'Ошибка при загрузке профиля',
|
||||
error: error,
|
||||
stackTrace: stackTrace,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
17
lib/features/profile/domain/bloc/profile_event.dart
Normal file
17
lib/features/profile/domain/bloc/profile_event.dart
Normal file
@@ -0,0 +1,17 @@
|
||||
part of 'profile_bloc.dart';
|
||||
|
||||
sealed class ProfileEvent extends Equatable {
|
||||
const ProfileEvent();
|
||||
|
||||
@override
|
||||
List<Object> get props => [];
|
||||
}
|
||||
|
||||
final class ProfileFetchProfileEvent extends ProfileEvent {
|
||||
final String id;
|
||||
|
||||
const ProfileFetchProfileEvent({required this.id});
|
||||
|
||||
@override
|
||||
List<Object> get props => [id];
|
||||
}
|
||||
36
lib/features/profile/domain/bloc/profile_state.dart
Normal file
36
lib/features/profile/domain/bloc/profile_state.dart
Normal file
@@ -0,0 +1,36 @@
|
||||
part of 'profile_bloc.dart';
|
||||
|
||||
sealed class ProfileState extends Equatable {
|
||||
const ProfileState();
|
||||
|
||||
@override
|
||||
List<Object> get props => [];
|
||||
}
|
||||
|
||||
final class ProfileInitialState extends ProfileState {}
|
||||
|
||||
final class ProfileWaitingState extends ProfileState {}
|
||||
|
||||
final class ProfileErrorState extends ProfileState {
|
||||
final String message;
|
||||
final Object error;
|
||||
final StackTrace? stackTrace;
|
||||
|
||||
const ProfileErrorState({
|
||||
required this.message,
|
||||
required this.error,
|
||||
this.stackTrace,
|
||||
});
|
||||
|
||||
@override
|
||||
List<Object> get props => [message, error];
|
||||
}
|
||||
|
||||
final class ProfileSuccessState extends ProfileState {
|
||||
final Object data;
|
||||
|
||||
const ProfileSuccessState({required this.data});
|
||||
|
||||
@override
|
||||
List<Object> get props => [data];
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
import 'package:friflex_starter/di/di_base_repo.dart';
|
||||
|
||||
/// {@template IProfileRepository}
|
||||
///
|
||||
/// {@endtemplate}
|
||||
abstract interface class IProfileRepository with DiBaseRepo {
|
||||
Future<String> fetchUserProfile(String id);
|
||||
}
|
||||
Reference in New Issue
Block a user