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}()
?
Asked
Active
Viewed 1,813 times
1

Sachihiro
- 1,597
- 2
- 20
- 46
1 Answers
1
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

Julio Henrique Bitencourt
- 1,752
- 14
- 18
-
1Thank you very, much, i used if... else definitely, that was a stupid idea. – Sachihiro Jun 18 '19 at 10:38