簡體   English   中英

state 在 flutter_bloc 中總是 null

[英]state is always null in flutter_bloc

我面臨這個奇怪的問題,雖然我已經設置了 state,但是在 init state 中訪問時的值顯示為 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();
  }

控制台日志:

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 rd@xddf.co
I/flutter ( 5029):  phone 12222222255

I/flutter ( 5029): I am inside the initState The value of userstate is null

雖然userState's userModel有價值,但為什么我不能在 initState.

我的嘗試:

我已經使用了 BlocListener,state 仍然是 null,我很困惑。

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>

看來問題是您正在使用 const UserState() 初始化 initState 方法中的 _userState 變量,它將 userModel 值設置為 null。這是在從 UserCubit 發出 UserExists state 之前發生的,它將 userModel 值設置為有效的用戶模型 object。

您應該通過調用 state getter 來使用 UserCubit 的當前 state 初始化 _userState。

@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();
}

在處理小部件后,在有狀態小部件的 dispose 方法中取消訂閱偵聽器也是最佳實踐。

@override
void dispose() {
    _userCubit.close();
    super.dispose();
}

請注意,上面的代碼假定您正在使用 bloc package 來管理您的應用程序的 state。

暫無
暫無

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

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