I am facing this weird issue, where though I have set the state, but the value when accessed in init state is showing null !!
user_cubit.dart
UserCubit() : super(UserInitialState()) {
emit(UserMainLoadingState());
_firestore
.collection("users")
.doc(_currentUser?.uid)
.snapshots()
.listen((event) {
event.exists
? {
emit(UserExists(
userModel: UserModel.fromjson(event.data()!, event.id)))
}
: {print("There is no such user"), emit(UserNotExists())};
});
}
user_state.dart
class UserState extends Equatable {
final UserModel? userModel;
const UserState({this.userModel});
@override
List<Object?> get props => [userModel];
}
class UserExists extends UserState {
UserExists({required UserModel userModel}) : super(userModel: userModel) { // Here the state is received
print("I am inside the UserExists state and the user is :$userModel");
}
}
myWidget.dart
@override
void initState() {
_userState = const UserState();
print("I am inside the initState The value of userstate is ${_userState.userModel}"); // This prints null , though i have set the state and console logs the state and it is confirmed that state exists why isn't this not null
if (_userState.userModel != null) {
print("user is ${_userState.userModel.toString()}");
}
super.initState();
}
Console log:
I/flutter ( 5029): I am inside the UserExists state and the user is :avatar boy
I/flutter ( 5029): fullName krrrt
I/flutter ( 5029): dob 19/1/2022
I/flutter ( 5029): email [email protected]
I/flutter ( 5029): phone 12222222255
I/flutter ( 5029): I am inside the initState The value of userstate is null
Though the userState's
userModel
has value, why can't i access that in the `initState.
My tries :
I have used BlocListener, still the state remains null, I'm hell confused.
body: BlocListener<UserCubit, UserState>(
listener: (context, state) {
print("I am inside the listener"); // this line is never printed
if (state.userModel != null) {
print("State is ${state.runtimeType}"); // But if the state has some value, why isn't this being printed !!!??
}
},
child: <My widget>
CodePudding user response:
It seems that the issue is that you are initializing the _userState variable in the initState method with const UserState(), which sets the userModel value to null. This is happening before the UserExists state is emitted from the UserCubit, which sets the userModel value to a valid UserModel object.
You should instead initialize _userState with the current state of the UserCubit by calling the state getter.
@override
void initState() {
_userCubit = context.bloc<UserCubit>();
_userState = _userCubit.state;
print("I am inside the initState The value of userstate is ${_userState.userModel}");
if (_userState.userModel != null) {
print("user is ${_userState.userModel.toString()}");
}
super.initState();
}
It would also be best practice to unsubscribe the listener after the widget is disposed, in the dispose method of your stateful widget.
@override
void dispose() {
_userCubit.close();
super.dispose();
}
Note that the above code assumes that you are using the bloc package to manage the state of your app.