operator [] method Null safety

dynamic operator [](
  1. Type t
)

Implementation

dynamic operator [](Type t) {
  //todo: optimize by keeping a cache where keys are of type [Type] to avoid the
  // expensive indexOf and substring calls in this method
  final typeName = t.toString();
  final r = map[typeName];
  if (r != null) {
    return r;
  }

  final genericIndex = typeName.indexOf("<");
  if (genericIndex == -1) {
    throw ArgumentError("Runtime not found for type '$t'.");
  }

  final genericTypeName = typeName.substring(0, genericIndex);
  final out = map[genericTypeName];
  if (out == null) {
    throw ArgumentError("Runtime not found for type '$t'.");
  }

  return out;
}