0

How to omit second condition in conditional statement?

    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        message != null
            ? Padding(
                padding: const EdgeInsets.all(24.0),
                child: Text(message),
              )
            : null, // how to omit second condition?
      ],
    );

Wheh the second condition is null I get

════════ Exception caught by widgets library ════════ The following assertion was thrown building ExportData(dirty, state: _ExportDataState#e723d): Column's children must not contain any null values, but a null value was found at index 4

rozerro
  • 5,787
  • 9
  • 46
  • 94

2 Answers2

3

Generally i would return a Container() in this case.


return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        message != null
            ? Padding(
                padding: const EdgeInsets.all(24.0),
                child: Text(message),
              )
            : Container(), // how to omit second condition?
      ],
    );

mahoriR
  • 4,377
  • 3
  • 18
  • 27
3

I prefer SizedBox instead of Container.

return Column(
  mainAxisAlignment: MainAxisAlignment.center,
  children: <Widget>[
    message != null
        ? Padding(
            padding: const EdgeInsets.all(24.0),
            child: Text(message),
          )
        : SizedBox(),
  ],
);
Vinoth Vino
  • 9,166
  • 3
  • 66
  • 70