How do I solve the 'Failed assertion: boolean expression must not be null' exception in Flutter

后端 未结 2 1289
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-11 20:48

I was working on a Flutter application and kept receiving this error String in the logcat.

Failed assertion: boolean expression must not be null
         


        
2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-11 21:18

    This problem occurs when one of your defined boolean type variable is not initialized with default value, and you try to use and assign it somewhere as value. example maybe you have you bool isEnabled ; is not defined as should bool isEnabled = false; or bool isEnabled = true; and you try to use it like readOnly: isEnabled,

    To avoid these to ensure isEnabled won't be null. Here is a kind of example of how we can avoid these

    bool isChecked = false;
    
    Checkbox(
      value: isChecked,  // initialized with `true/false` also avoids the error,
      onChanged ...
    )
    

提交回复
热议问题