1

For a cross-platform (iOS, android) project I consider using Dart with UI-Programming in Flutter.

For straight-forward translation of present Kotlin code, I want to call functions by their function name. To be more precise, I am looking for how to write the function getFunctionByName such that the following code prints "The answer is 42."?

void function42(String s) {
    print(s + " 42.");
}

void main() {
    int number = 42;
    String calculatedFunctionName = "function" + number.toString();
    String calculatedArgument = "The answer is";
    Function f = getFunctionByName(calculatedFunctionName);
    f(calculatedArgument);
}

What I try to achieve works in Kotlin and seems to work in Dart, but when I tried the linked Dart code in a Flutter project, the import

import 'dart:mirrors';

does not work for me. There is a whole thread about reflection in Flutter, which I interpret such that at the time of writing reflection does not work in Flutter. But maybe there is a workaround for the specific thing I want to do?

CryptUser
  • 608
  • 1
  • 8
  • 17

1 Answers1

2

This is not possible in Flutter. Flutter disable dart:mirror to make binaries smaller.

Instead flutter (and now angulardart) uses code generation as an alternative.

Dart comes with a really powerful code generators tool : build

Rémi Rousselet
  • 256,336
  • 79
  • 519
  • 432
  • Thank you for your answer. Searching for code generation, I found the reflectable library for Flutter (https://pub.dartlang.org/packages/reflectable). While I did not get the code running yet, it sounds like it should be possible with this library. – CryptUser Jun 30 '18 at 19:36
  • Getting Reflectable to run with Flutter turned out to be more difficult than I thought. I opened a new question that focusses on Reflectable: https://stackoverflow.com/questions/51169706/flutter-reflection-with-reflectable-working-example-needed – CryptUser Jul 04 '18 at 09:19