Search code examples
flutterdartbloccubit

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

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?


Solution

  • flutter pub cache repair
    

    delete pubspec.lock then

    flutter pub get
    

    Thanks to Rolly Mod