Skip to content

Instantly share code, notes, and snippets.

@alexcmgit
Last active April 13, 2021 18:12
Show Gist options
  • Save alexcmgit/f10661370488191b57ab064fc1fe217d to your computer and use it in GitHub Desktop.
Save alexcmgit/f10661370488191b57ab064fc1fe217d to your computer and use it in GitHub Desktop.
void main() {
final dynamicInstance = createInstanceOf<Foo>();
print(dynamicInstance);
}
T? createInstanceOf<T>() {
final factories = <Type, T Function()>{
Foo: () => Foo() as T,
Bar: () => Bar() as T,
};
final createInstance = factories[T];
return createInstance?.call(); // Same as createInstance() but null safety
}
class Foo {}
class Bar {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment