【问题标题】:flutter bloc(cubit) state is not defined?颤动块(cubit)状态未定义?
【发布时间】:2021-11-30 12:26:41
【问题描述】:
  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,
    );
  }
}

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

【问题讨论】:

    标签: flutter dart bloc cubit


    【解决方案1】:

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

    尝试将其作为参数传递

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

    }

    【讨论】:

    • 还是全局变量?
    • 这不是未知数。我解决了,看上面。有时酒吧表现得很疯狂:))
    【解决方案2】:
    flutter pub cache repair
    

    然后删除 pubspec.lock

    flutter pub get
    

    感谢 Rolly Mod

    【讨论】:

    • 或者flutter clean,然后退出vscode。在那个酒吧之后得到
    猜你喜欢
    • 2022-01-25
    • 2020-11-28
    • 1970-01-01
    • 2019-12-11
    • 2021-09-03
    • 2018-09-22
    • 2022-10-13
    • 2021-02-01
    • 2022-11-16
    相关资源
    最近更新 更多