tryToMap function

dynamic tryToMap(
  1. dynamic object
)

Returns object.toMap() if it exists, otherwise the object itself. Only swallows NoSuchMethodError from a missing toMap member — any exception thrown by a present toMap propagates to the caller so data-integrity bugs are not hidden.

Implementation

dynamic tryToMap(dynamic object) {
  if (object == null) return null;
  try {
    return object.toMap();
  } on NoSuchMethodError {
    return object;
  }
}