MaybeWidget<T extends Object> constructor

const MaybeWidget<T extends Object>(
  1. T? value,
  2. Widget _builder(
    1. T
    ), {
  3. Widget orElse = const SizedBox.shrink(),
  4. bool buildWhen(
    1. T
    )?,
  5. Key? key,
})

Constructor for the MaybeWidget class.

  • value is the value to check for null.
  • _builder is the builder function to call with a non-null value.
  • orElse is the widget to display if value is null.
  • buildWhen optional predicate; if provided and returns true the builder runs, if it returns false orElse is used. When omitted only the null-check is applied.
  • key is the key for the widget. Example:
MaybeWidget(
 nullableText,
 (text) => Text(text),
 buildWhen: (text) => text.trim().isNotEmpty, // Only for non-empty text.
 orElse: const Icon(Icons.text_decrease),
),

Implementation

const MaybeWidget(
  this.value,
  this._builder, {
  this.orElse = const SizedBox.shrink(),
  this.buildWhen,
  super.key,
});