entityFromArgs<T> function

T? entityFromArgs<T>(
  1. Map<String, dynamic>? args,
  2. T fromJson(
    1. Map<String, dynamic>
    )
)

Build a typed entity from a chat directive's flat params map.

Used by widget-registry builders so an agent can open a create/edit dialog pre-filled with field values, e.g. {"action":"dialog","widget":"UserDialog","params":{"firstName":"John"}}. Reserved navigation keys are stripped and every remaining value is passed as a String, because GrowERP model converters deserialize from strings (e.g. Decimal.fromJson(json['price'] as String), DateTime). Sending a raw number would throw the as String cast and lose the whole prefill. Returns null when there are no usable field values (so callers keep their default).

Implementation

T? entityFromArgs<T>(
  Map<String, dynamic>? args,
  T Function(Map<String, dynamic>) fromJson,
) {
  if (args == null) return null;
  final fields = <String, dynamic>{};
  args.forEach((k, v) {
    if (_reservedArgKeys.contains(k) || v == null) return;
    fields[k] = v is String ? v : v.toString();
  });
  if (fields.isEmpty) return null;
  try {
    return fromJson(fields);
  } catch (_) {
    return null; // unknown field types — fall back to caller default
  }
}