簡體   English   中英

flutter 塊(肘)state 未定義?

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

這是我的簡單 function。State 給出錯誤我不知道為什么。 所有包都已安裝 (flutter_bloc)。 為什么會報錯?

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

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

昨天沒報錯,是package壞了嗎?

flutter pub cache repair

然后刪除 pubspec.lock

flutter pub get

感謝 Rolly Mod

我認為在 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]),
));

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM