用StateNotifier处理多个变量

更新时间:2023-05-25 21:55
要用StateNotifier处理多个变量,需要将它们放到类中。
class ValidationItem {
  final String value;
  final String error;

  const ValidationItem(this.value, this.error);
}

class ValidationItems {
  final ValidationItem email;
  final ValidationItem password;

  ValidationItems({
    this.email = const ValidationItem(null, null),
    this.password = const ValidationItem(null, null),
  });

  ValidationItems copyWith({
    ValidationItem email,
    ValidationItem password,
  }) {
    return ValidationItems(
      email: email ?? this.email,
      password: password ?? this.password,
    );
  }
}
然后你可以在StateNotifier内部这样处理。
class ValidationNotifier extends StateNotifier<ValidationItems> with Validator {
  ValidationNotifier() : super(ValidationItems());

  void updateEmail(String value) {
    if (emailValidator.isValid(value)) {
      state = state.copyWith(email: ValidationItem(value, null));
    } else {
      final error = emailValidator.error(value);
      state = state.copyWith(email: ValidationItem(null, error));
    }
  }

  void updatePassword(String value) {
    if (passwordValidator.isValid(value)) {
      state = state.copyWith(password: ValidationItem(value, null));
    } else {
      final error = passwordValidator.error(value);
      state = state.copyWith(password: ValidationItem(null, error));
    }
  }

  void validate() {
    if (!passing) return; 
    debugPrint('''
    User saved in the DB.
    User: ${state.email.value}
    Password: ${state.password.value}
    ''');
  }

  bool get passing {
    if (!emailValidator.isValid(state.email.value) ||
        !passwordValidator.isValid(state.password.value)) {
      return false;
    } else {
      return true;
    }
  }
}
StateNotifier可能看起来更高级,像ChangeNotifier,但使用StateNotifier,你被迫创建更安全的代码。但实际上,你可以用两者做同样的事情。
例如,如果您对如何处理多个Map变量感兴趣。然后看看这个例子。
 
class Member {
  const Member({
    this.admin = const {},
    this.moderator = const {},
    this.user = const {},
  });

  final Map<String, String> admin;
  final Map<String, String> moderator;
  final Map<String, String> user;

  Member copyWith({
    Map<String, String> admin,
    Map<String, String> moderator,
    Map<String, String> user,
  }) {
    return Member(
      admin: admin ?? this.admin,
      moderator: moderator ?? this.moderator,
      user: user ?? this.user,
    );
  }
}
class MemberNotifier extends StateNotifier<Member> {
  MemberNotifier() : super(Member());

  void updateAdmin(String id, String name) {
    final admin = Map<String, String>.from(state.admin); // Convert it to editable Map.
    admin.update(id, (_) => name, ifAbsent: () => name); // Update map.

    state = state.copyWith(admin: admin); // Update state.
  }

  void updateModerator(String id, String name) {
    final moderator = Map<String, String>.from(state.moderator);
    moderator.update(id, (_) => name, ifAbsent: () => name);

    state = state.copyWith(moderator: moderator);
  }

  void updateUser(String id, String name) {
    final user = Map<String, String>.from(state.user);
    user.update(id, (_) => name, ifAbsent: () => name);

    state = state.copyWith(user: user);
  }

  void removeAdmin(String id) {
    state.admin.remove(id);
  }

  void removeModerator(String id) {
    state.moderator.remove(id);
  }

  void removeUser(String id) {
    state.user.remove(id);
  }
}

使用StateNotifier从互联网安全处理状态数据