flutter bloc(cubit) 状态未定义?

flutter bloc(cubit) state is not defined?

  void emailChanged(String value) {
    final email = Email.dirty(value);
    emit(state.copyWith(
      email: email,
      status: Formz.validate([email, state.password]),
    ));
  }

这是我的简单功能。状态给出错误我不知道为什么。所有软件包都已安装 (flutter_bloc)。为什么会报错?

Undefined name 'state'.
Try correcting the name to one that is defined, or defining the name

我的状态:

part of 'login_cubit.dart';

class LoginState extends Equatable {
  const LoginState({
    this.email = const Email.pure(),
    this.password = const Password.pure(),
    this.username = const Username.pure(),
    this.status = FormzStatus.pure,
    this.exceptionError = "",
  });

  final Email email;
  final Username username;
  final Password password;
  final FormzStatus status;
  final String exceptionError;

  @override
  List<Object> get props =>
      [email, username, password, status, exceptionError];

  LoginState copyWith({
    Email? email,
    Username? username,
    Password? password,
    FormzStatus? status,
    String? exceptionError,
  }) {
    return LoginState(
      email: email ?? this.email,
      username: username ?? this.username,
      password: password ?? this.password,
      status: status ?? this.status,
      exceptionError: exceptionError ?? this.exceptionError,
    );
  }
}

昨天没报错,是不是包坏了?

我认为在 emailChanged() 的独家新闻中,您尝试发出的变量 'state' 是未知的。

尝试将其作为参数传递

void emailChanged(String value, <YOUR_TIPE> state) {
final email = Email.dirty(value);
emit(state.copyWith(
  email: email,
  status: Formz.validate([email, state.password]),
));

}

flutter pub cache repair

删除pubspec.lock然后

flutter pub get

感谢 Rolly Mod