I have 2 methods, tappedNext()
and tappedPrevious()
, i need to call this function by passing a string variable, e.g onTap: tapped + part + ()
, is it possible to make a string interpolation for this like tapped${part}()
?
I certainly do not recommend you to do that. Instead, use a simple if statement. But since you asked if it is possible..
Short answer, no (with a function only). Unless you're talking about methods, in that case we could use some mirrors.
You'll need to import the mirror package, it doesn't work on dartpad, so test it locally:
import 'dart:mirrors';
class MyClass {
tappedNext() {
print('Next Function');
}
tappedPrevious() {
print('Previous Function');
}
}
Now you must create an object from the class, create a mirror of it, and then make use of a Symbol
to call the method in a reflectly way.
void main() {
final clazz = MyClass();
final mirror = reflect(clazz);
final function = 'Next';
Symbol s = Symbol('tapped$function');
mirror.invoke(s, []).reflectee;
}
That's it, the console will print:
Next Function