如何访问Flutter Bloc中的变量,而不将它们作为参数传递给每个状态?

ktca8awb  于 2023-04-07  发布在  Flutter
关注(0)|答案(4)|浏览(87)

所以我有一个Flutter应用程序,使用Bloc进行状态管理。我想在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));
        });
      }
0g0grzrc

0g0grzrc1#

如果你不需要在所有的bloc状态中定义usersuser,你不必在UserState中定义它们。你可以在UsersLoadedStateFriendAddedState中这样定义它们:

@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;
}

**[推荐]**如果你想访问bloc的每个状态下的user,你可以为你的bloc状态使用一个类,并使用一个枚举来定义它的状态:

@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()

l0oc07j2

l0oc07j22#

您可以用途:context.read();
BlocProvider.of(context);
希望这能帮上忙。

xam8gpfp

xam8gpfp3#

您可以尝试使用两个不同的块,一个用于状态,一个用于用户列表。
另一种方法是只使用一个state类,并为所有可能的状态创建一个枚举。这样,您就不会在每次状态更改时重新初始化该类。类似于:

enum UserState {Initial, Loaded, Pending..}

class UsersState {
  UserState? state;
  List<User> users;
  User? user;

  const UsersState({required this.users, required this.user});
}
fae0ux8s

fae0ux8s4#

您可以将用户设置为可选的,这样就不会在每个状态下都需要它们。

无空安全

@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);
}

相关问题