coerceNestedList<T> static method

List<List<T>> coerceNestedList<T>(
  1. Object? arg,
  2. String paramName
)

Coerce a nested List<List<T>> from D4rt.

For parameters like List<List<Widget>>, the outer list contains inner lists that each need element-level coercion. Standard coerceList<List<T>> can't handle this because inner lists are List<dynamic> which can't be cast to List<T> directly.

Implementation

static List<List<T>> coerceNestedList<T>(Object? arg, String paramName) {
  if (arg == null) {
    throw ArgumentD4rtException(
      'Invalid parameter "$paramName": expected List<List<$T>>, got null',
    );
  }

  final value = arg is BridgedInstance ? arg.nativeObject : arg;

  if (value is! List) {
    throw ArgumentD4rtException(
      'Invalid parameter "$paramName": expected List<List<$T>>, '
      'got ${value.runtimeType}',
    );
  }

  if (value is List<List<T>>) return value;

  try {
    return value.map<List<T>>((row) {
      return coerceList<T>(row, paramName);
    }).toList();
  } catch (e) {
    throw ArgumentD4rtException(
      'Invalid parameter "$paramName": cannot convert to '
      'List<List<$T>> - $e',
    );
  }
}