[英]Why state is not updating in cubits?
I am new at flutter ant trying to use bloc/cubit我是 flutter ant 的新人,正在尝试使用 bloc/cubit
I have a state inside cubit file and when I try to emit state... state is not changing and I don't understand why我在 cubit 文件中有一个 state,当我尝试发出 state 时...state 没有改变,我不明白为什么
This is my cubit file:这是我的肘文件:
//auth_cubit.dart //auth_cubit.dart
import 'package:flutter_bloc/flutter_bloc.dart';
part of 'auth_state.dart'; // here ide returns an error: The part-of directive must be the only directive in a part.
class AuthCubit extends Cubit<AuthState> { // The name 'AuthState' isn't a type so it can't be used as a type argument.
AuthCubit() : super(AuthState(
email: "Log in",
password: null,
firstName: "",
lastName: "",
genderId: 0,
ageGroupId: 0,
countryUuid: 0
));
void setCountryUuid(int countryUuid) => emit(AuthState(countryUuid: countryUuid));
}
//auth_state.dart //auth_state.dart
part of 'auth_cubit.dart';
class AuthState {
final email;
final password;
final firstName;
final lastName;
final genderId;
final ageGroupId;
final countryUuid;
const AuthState({
this.email, //string
this.password, //string
this.firstName, //string
this.lastName, //string
this.genderId, //int
this.ageGroupId, //int
this.countryUuid //int
});
}
Why does state cannot connect to cubit?为什么state无法连接到cubit?
There is error to fix first, see above on the part part (ahah):有错误先修复,看上面部分(啊哈):
import 'package:flutter_bloc/flutter_bloc.dart';
part 'auth_state.dart'; // Just use part in the cubit
class AuthCubit extends Cubit<AuthState> { // This error should be gone
AuthCubit() : super(AuthState(
email: "Log in",
password: null,
firstName: "",
lastName: "",
genderId: 0,
ageGroupId: 0,
countryUuid: 0
));
void setCountryUuid(int countryUuid) => emit(AuthState(countryUuid: countryUuid));
}
After that, your cubit should be able to emit new state.在那之后,你的手肘应该能够发出新的 state。
Using equatable will help for value equality.使用equatable将有助于价值平等。
By default, == returns true if two objects are the same instance.默认情况下,如果两个对象是同一个实例,== 返回 true。
class AuthState extends Equatable {
final String? email;
final String? password;
final String? firstName;
final String? lastName;
final int? genderId;
final int? ageGroupId;
final int? countryUuid;
const AuthState({
this.email,
this.password,
this.firstName,
this.lastName,
this.genderId,
this.ageGroupId,
this.countryUuid,
});
AuthState copyWith({
String? email,
String? password,
String? firstName,
String? lastName,
int? genderId,
int? ageGroupId,
int? countryUuid,
}) {
return AuthState(
email: email ?? this.email,
password: password ?? this.password,
firstName: firstName ?? this.firstName,
lastName: lastName ?? this.lastName,
genderId: genderId ?? this.genderId,
ageGroupId: ageGroupId ?? this.ageGroupId,
countryUuid: countryUuid ?? this.countryUuid,
);
}
@override
List<Object?> get props =>
[email, password, firstName, lastName, genderId, ageGroupId, countryUuid];
}
emit new instance to update the UI.发出新实例以更新 UI。
emit(state.copyWith(countryUuid: countryUuid));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.