所以我有一个用于状态管理的Flutter应用程序。我想在Bloc中保留一个用户列表,这样我就可以从应用程序的不同位置访问它。但现在我将这个用户列表传递给每个州。我能以某种方式避免这个样板代码吗?
下面是我的Bloc State的一个例子:
@immutable
abstract class UsersState {
final List<User> users;
final User? user;
const UsersState({required this.users, required this.user});
}
class UsersInitial extends UsersState {
UsersInitial() : super(users: [], user: null);
}
class UsersLoadedState extends UsersState {
const UsersLoadedState({required List<User> users, required User? user})
: super(users: users, user: user);
}
class UsersPendingState extends UsersState {
const UsersPendingState({List<User> users = const [], User? user})
: super(users: users, user: user);
}
class UsersErrorState extends UsersState {
final String message;
const UsersErrorState(
{required List<User> users, required User? user, required this.message})
: super(users: users, user: user);
}
class FriendAddedState extends UsersState {
const FriendAddedState({required List<User> users, required User? user})
: super(users: users, user: user);
}
在本例中,我不想将用户列表传递给FriendAddedState。下面是它在Bloc中的样子:
else if (event is AddFriend) {
emit(FriendAddPending(users: users, user: user));
final result = await usecases.addFriend(event.friendId);
result.fold((error) {
emit(UsersErrorState(
users: users, user: user, message: error.message));
}, (success) {
user = success;
emit(FriendAddedState(users: users, user: user));
});
}
如果您在所有集团状态中都不需要users
和user
,则不必在UserState
中定义它们。您可以在UsersLoadedState
FriendAddedState
中这样定义它们:
@immutable
abstract class UsersState {
const UsersState();
}
class UsersInitial extends UsersState {
const UsersInitial();
}
class UsersLoadedState extends UsersState {
const UsersLoadedState({
required this.users,
required this.user,
});
final List<User> users;
final User? user;
}
class UsersPendingState extends UsersState {
const UsersPendingState();
}
class UsersErrorState extends UsersState {
final String message;
const UsersErrorState({required this.message});
}
class FriendAddedState extends UsersState {
const FriendAddedState({
required this.users,
required this.user,
});
final List<User> users;
final User? user;
}
[推荐]如果你想在每个状态下访问user,你可以用一个类来定义你的状态,用一个enum来定义它的状态:
@immutable
class UsersState {
final List<User> users;
final User? user;
final String? message;
final UserStatus status;
const UsersState({
required this.status,
required this.users,
this.user,
this.message,
});
const UsersState.initial()
: status = UserStatus.initial,
users = const [],
user = null,
message = null;
UsersState copyWith(
List<User>? users,
User? user,
String? message,
UserStatus? status,
) {
return UsersState(
status: status ?? this.status,
users: users ?? this.users,
message: message ?? this.message,
user: user ?? this.user,
);
}
}
enum UserStatus {
initial,
loaded,
pending,
error,
friendAdded,
}
然后在你的bloc类中,任何你想发出新状态的地方,使用state.copyWith()
!
你可以使用:context.read
或BlocProvider.of
希望这对你有帮助。
您可以尝试使用两个不同的块,一个用于状态,一个用于用户列表。
另一种选择是只有一个状态类,并为所有可能的状态提供enum。这样就不必在每次状态改变时都重新初始化类。比如:
enum UserState {Initial, Loaded, Pending..}
class UsersState {
UserState? state;
List<User> users;
User? user;
const UsersState({required this.users, required this.user});
}
你可以将用户设置为可选的,这样在每个状态下都不是必需的。
无null安全
@immutable
abstract class UsersState {
final List<User>? users;
final User? user;
const UsersState({required this.users, this.user});
}
class UsersInitial extends UsersState {
UsersInitial() : super(users: [], user: null);
}
class UsersLoadedState extends UsersState {
const UsersLoadedState({required List<User> users, required User? user})
: super(users: users, user: user);
}
class UsersPendingState extends UsersState {
const UsersPendingState({List<User> users = const [], User? user})
: super(users: users, user: user);
}
class UsersErrorState extends UsersState {
final String message;
const UsersErrorState(
{required List<User> users, required User? user, required this.message})
: super(users: users, user: user);
}
class FriendAddedState extends UsersState {
const FriendAddedState({required User? user})
: super(user: user);
}
@immutable
abstract class UsersState {
final List<User>? users;
final User? user;
const UsersState({required this.users, this.user=[]});
}
class UsersInitial extends UsersState {
UsersInitial() : super(users: [], user: null);
}
class UsersLoadedState extends UsersState {
const UsersLoadedState({required List<User> users, required User? user})
: super(users: users, user: user);
}
class UsersPendingState extends UsersState {
const UsersPendingState({List<User> users = const [], User? user})
: super(users: users, user: user);
}
class UsersErrorState extends UsersState {
final String message;
const UsersErrorState(
{required List<User> users, required User? user, required this.message})
: super(users: users, user: user);
}
class FriendAddedState extends UsersState {
const FriendAddedState({required User? user})
: super(user: user);
}