#连接库初步完成
This commit is contained in:
30
lib/features/auth/presentation/bloc/app_user_cubit.dart
Normal file
30
lib/features/auth/presentation/bloc/app_user_cubit.dart
Normal file
@@ -0,0 +1,30 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
import '../../data/datasources/i_auth_local_datasource.dart';
|
||||
import '../../domain/entities/user_entity.dart';
|
||||
import 'app_user_state.dart';
|
||||
|
||||
class AppUserCubit extends Cubit<AppUserState> {
|
||||
// 注入你的 UserStorage (之前在 core/storage 里的那个)
|
||||
final IAuthLocalDataSource _storage;
|
||||
|
||||
AppUserCubit(this._storage) : super(const AppUserState()) {
|
||||
// 初始化时尝试从本地加载
|
||||
_loadFromStorage();
|
||||
}
|
||||
|
||||
Future<void> _loadFromStorage() async {
|
||||
final savedUser = await _storage.getUser();
|
||||
if (savedUser != null) emit(AppUserState(user: savedUser));
|
||||
}
|
||||
|
||||
void setAuth(UserEntity user) {
|
||||
_storage.saveUser(user); // 持久化
|
||||
emit(AppUserState(user: user));
|
||||
}
|
||||
|
||||
void clearAuth() {
|
||||
_storage.deleteUser(); // 清除本地数据
|
||||
emit(const AppUserState());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user