normalizeChildren function

List<ReactNode> normalizeChildren(
  1. ReactChildren children
)

Normalizes Dart-friendly child values into the portable React node model.

Supported values are ReactNode, String, num, nested Iterable values, null, and booleans. Like React, null and booleans render nothing. Unsupported values fail early with a descriptive error.

Implementation

List<ReactNode> normalizeChildren(ReactChildren children) {
  final normalized = <ReactNode>[];

  void append(Object? child) {
    switch (child) {
      case null || bool():
        return;
      case ReactNode():
        normalized.add(child);
      case String():
        normalized.add(Text(child));
      case num():
        normalized.add(Text('$child'));
      case Iterable<Object?>():
        for (final nested in child) {
          append(nested);
        }
      default:
        throw ArgumentError.value(
          child,
          'children',
          'Expected a ReactNode, String, number, boolean, null, or Iterable.',
        );
    }
  }

  for (final child in children) {
    append(child);
  }
  return normalized;
}