getType static method

Type? getType(
  1. String name, [
  2. String? library
])

Gets object type by its name and library where it is defined.

  • name an object type name.
  • library a library where the type is defined Returns the object type or null is the type wasn't found.

Implementation

static Type? getType(String name, [String? library]) {
  // If library is not defined then scan all loaded libraries
  if (library == null) {
    for (var lib in currentMirrorSystem().libraries.values) {
      var result = _findType(lib, name);
      if (result != null) return result;
    }
    return null;
  }

  // Load library dynamically
  try {
    Uri libraryUri;

    // When library defined as URI
    if (library.startsWith('package:') ||
        library.startsWith('dart:') ||
        library.startsWith('file:')) {
      libraryUri = Uri.parse(library);
    }
    // Otherwise treat it as a file
    else {
      // Add current directory to the relative path
      if (library.startsWith('.')) {
        library = Directory.current.path + '/' + library;
      }
      libraryUri = Uri.file(library);
    }

    // Load the library synchronously
    var lib = waitFor(currentMirrorSystem().isolate.loadUri(libraryUri));
    var result = _findType(lib, name);

    return result;
  } catch (ex) {
    return null;
  }
}