Home > Mobile >  flutter bloc(cubit) state is not defined?
flutter bloc(cubit) state is not defined?

Time:10-12

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

here it is my simple function. State is giving error I do not know why. All packages already installed (flutter_bloc). Why it gives an error ?

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

my state:

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

yesterday it did not give an error, is package broken?

CodePudding user response:

I think in the scoop of emailChanged() the variable 'state', you're trying to emit, is unknown.

Try passing it as argument

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

}

CodePudding user response:

flutter pub cache repair

delete pubspec.lock then

flutter pub get

Thanks to Rolly Mod

  • Related