Flutter - How to instantiate an object from a Type
Solution 1
You cannot create an instance of a class from a Type
object representing the class, at least not without mirrors (and neither the Flutter nor the web platform supports mirrors).
You cannot tear-off a constructor (yet, I hope we'll add that feature at some point), so the option of creating a static helper function which calls the constructor, and passing the helper function around, is the best current solution.
If you know the number of arguments required by such a function, you don't need to use Function.apply
, you can just call it directly.
Solution 2
You can tear-off a function if you don't add the parentheses ()
. You don't need dart:mirror
.
void main() {
// Tear off a global function
Function myFunction = MyClass.someStaticFunction;
// Call it statically
myFunction('arg');
// Call it dynamically
Function.apply(myFunction, ['arg']);
// Tear it off with a precise Function Type
void Function(String) myFunction2 = MyClass.someStaticFunction;
}
class MyClass {
static void someStaticFunction(String someArg) {
print('someStaticFunction $someArg');
}
}
Solution 3
If you don't want to user reflection, you can do this.
class A {
}
class B {
}
dynamic init(Type cls){
switch (cls) {
case A:
return A();
case B:
return B();
}
}
void main() {
print(init(A));
print(init(B));
}
As a best practice, to keep organized and avoid using dynamic, you could extend from a common base and call a static constructor.
class A extends Base{
}
class B extends Base{
}
abstract class Base{
static Base init(Type cls){
switch (cls){
case A:
return A();
case B:
return B();
}}
}
void main() {
print(Base.init(A));
print(Base.init(B));
}
Output:
Instance of 'A'
Instance of 'B'
Process finished with exit code 0
Related videos on Youtube
stackunderflow
Updated on September 14, 2022Comments
-
stackunderflow over 1 year
My aplogoze for the original question. I'm getting confused. This is my actual question.
I have a situation where I need to use some defined classes as an object of type
Type
and instantiate it's object instance. As for now, I'm using self defined static instantiator function to call the constructor and it's working good. So I can just callFunction.apply()
to create an object. Rather than doing that, is there any way I can use the class'sType
to emulate the class's constructor.
What have I tried? They say to use
Mirror
, a solution for Dart's reflection.import 'dart:mirrors'
have failed with errorTarget of URI doesn't exists
Or at least if it's not ready provided I don't know where to get the package.
import 'package:reflectable/mirrors.dart'
with package from git://github.com/dart-lang/reflectable.git only defines allMirror
implemented classes, but functionreflect
is missing.-
Günter Zöchbauer over 5 yearsCan you provide a concrete example of what you try to accomplish and at what step you're stuck?
-
Günter Zöchbauer over 5 yearsMirrors is not available in Flutter.
-
-
stackunderflow over 5 yearsi'm sorry sir. original question actually my mistake. i've updated it. +1 for your answer
-
stackunderflow over 5 yearsi'm sorry sir. original question actually my mistake. i've updated it. +1 for your answer
-
lrn over 5 yearsMakes sense, I've updated the answer (rewritten, more like).
-
Eric Platon about 3 yearsAny risk of circularity in this alternative? When sub-classes are declared in a different file, the two files need to import each other.