In dart is it possible to instantiate a class from a string?
For example:
- vanilla in javascript:
var myObject = window[classNameString];
- Objective-C:
id myclass = [[NSClassFromString(@"MyClass") alloc] init];
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You need to know the library name and class name to make things work properly. Assume you know both, the below example will instantiate the
TestClassand calldoStuffon it.A few notes about this solution:
testwe are trying to load the class from is already imported in the VM, which makes this case a bit easier.newInstanceallows for passing parameters to the constructor. Positional arguments are implemented, but named parameters are not yet implemented (as of the M2 release).newInstancereturns a Future to allow it to work across isolates.