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

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

Failed assertion: boolean expression must not be null

Here is the code in question:

@override
Widget build(BuildContext context) {
    return Center(
        child: ListView(
            children: <Widget>[
                TextField(
                    controller: controller,
                    decoration: InputDecoration(
                        hintText: "Type in something..."
                    ),
                ),
                RaisedButton(
                    child: Text("Submit"),
                    onPressed: () => addString(),
                ),
                Flex(
                    direction: Axis.vertical,
                    children: (list_two = null) ? [] :
                    list_two.map((String s) => Text(s)).toList() 
                )
            ],
        ),
    );
}

What is causing the problem?



Solution 1:[1]

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 ...
)

Solution 2:[2]

this error often caused by when a bool variable is checked using = operator

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Paresh Mangukiya
Solution 2 Farman Ullah Khan