MaybeWidget<T extends Object>.identifiable constructor

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

Identifiable constructor for the MaybeWidget class.

Creates a MaybeWidget with a key identifier. This ensures that the widget will always have a different key than the one with a null value. Typically used with animated parents.

  • 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. If key is not provided, a null-check key will be calculated and used.

Example:

AnimatedSwitcher(
 duration: const Duration(milliseconds: 800),
 child: MaybeWidget.identifiable(
 nullableValue,
 (nonNullValue) => Text(nonNullValue.toString()),
  ),
),

Implementation

MaybeWidget.identifiable(
  this.value,
  this._builder, {
  this.orElse = const SizedBox.shrink(),
  this.buildWhen,
  Key? key,
}) : super(key: key ?? ValueKey<bool>(value != null));