簡體   English   中英

Flutter bloc 和 null 安全實施

[英]Flutter bloc and null safety implementation

我正在實施 Null Safety to Bloc Patter in flutter,一切正常,但是當我嘗試在我的小部件中實例化 bloc 時,我收到一個錯誤“Null check operator used on a Z37A6259CC0C1DAE299A7866489DFF0事件

class LoginEvent extends InheritedWidget{
  final LoginBloc loginBloc = LoginBloc();

  LoginEvent({required Widget child}) : super(child: child);
  // This function has the error
  static LoginBloc of(BuildContext context){
    return context.dependOnInheritedWidgetOfExactType<LoginEvent>()!.loginBloc;
  }

  @override
  bool updateShouldNotify(covariant InheritedWidget oldWidget) => true;
}

如果我刪除“。” 出現的消息是“無法無條件訪問屬性‘loginBloc’,因為接收者可以為‘null’。”

這是我的集團 class

class LoginBloc with LoginState{
  final _emailController = BehaviorSubject<String>();
  final _passwordController = BehaviorSubject<String>();

  Stream<String>get emailStream => _emailController.stream.transform(checkEmail);
  Stream<String>get passwordStream => _passwordController.stream.transform(checkEmail);

  Stream<bool> get formValidStream => Rx.combineLatest2(emailStream,passwordStream, (e,p) => true);

  Function(String) get changeEmail => _emailController.sink.add;
  Function(String) get changePassword => _passwordController.sink.add;

  String get email => _emailController.value;
  String get password => _passwordController.value;

  dispose(){
    _emailController.close();
    _passwordController.close();
  }
}

快速預覽它的調用方式

  @override
  Widget build(BuildContext context) {
    var height =
        MediaQuery.of(context).size.height - MediaQuery.of(context).padding.top;
    var space = height > 650 ? DesignSpacings.spaceM : DesignSpacings.spaceS;
    
    final bloc = LoginEvent.of(context); // if this line is commented the app shows the widget again

    return Card(...);
  }

您可以更改符號'!' '?' .

暫無
暫無

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

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