Last active
April 13, 2021 18:12
-
-
Save alexcmgit/f10661370488191b57ab064fc1fe217d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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