3

I wanna create a dynamic ListView and each item has a onTap property from a json configuration file.

Rest of the code runs properly.

How I do to Call this string stored command on a onTap property?

This Method retrieve the menu loaded and mount the Listview

  Widget _itemBuilder(BuildContext context, int index) {
    Menu myMenu = Menu(menu[index]);
    return new ListTile(
      leading: new Text("-"),
      title: new Text("Comando ${myMenu.name}"),
      onTap: <myMneu.command>,
    );
  }

json file

{
   "menu": [
      {
         "id": 1,
         "name": "Start",
         "command": "StartScreen()"
      },
      {
         "id": 2,
         "name": "Pause",
         "command": "PauseScreen()"
      },
      {
         "id": 3,
         "name": "Finish",
         "command": "FinishScreen()"
      }
   ]
}

2 Answers2

1

You will have to map the command strings to actual method calls:

  void executeCommand(String cmd) {
    switch(cmd) {
      case 'startScreen':
        startScreen();
        break;
      case 'pauseScreen':
        pauseScreen();
        break;
      case 'finishScreen':
        finishScreen();
        break;
    }
  }
boformer
  • 28,207
  • 10
  • 81
  • 66
  • This solution gonna work, the problem is implement a new case in every item added to json. – Anderson Figueredo Gomes Dec 16 '18 at 11:04
  • The only way around is code generation where you generate above code automatically using a code generation script. – Günter Zöchbauer Dec 16 '18 at 11:55
  • @GünterZöchbauer do you have any examples? I've search for code generation but don't understanding how it works. – Anderson Figueredo Gomes Dec 16 '18 at 12:10
  • There are different ways, depending on where the data for code generation comes from. You can write a simple Dart script that generates output when you invoke the script. You can use the `build` package if you want other code to be updated when some source files were modified. The build package is a bit cumbersome to start with, but if you use it more it will soon pay off. https://pub.dartlang.org/packages/json_serializable used the build package. – Günter Zöchbauer Dec 16 '18 at 12:14
  • Seems to be a elegant solution, but the question is: Can I generate and call the script at execution time. I need research more about this... thank you @GünterZöchbauer! – Anderson Figueredo Gomes Dec 16 '18 at 12:17
  • No, at latest at build time. If you want to do it at runtime you would at least need a map from String to function reference for all functions that should be supported. – Günter Zöchbauer Dec 16 '18 at 12:22
  • I almost forgot https://github.com/dart-lang/reflectable which generates non-mirrors code for code that uses mirrors. See also https://github.com/dart-lang/reflectable/issues/117 – Günter Zöchbauer Dec 16 '18 at 12:23
0

This will work

List callbacks = [
{
  'name': "test1",
  'callback': test,
},
{
  'name': "test2",
  'callback': test,
}
];

static test() {
    print("TIMER TEST");
}

Function.apply(callbacks['callback'], []);
// or
callbacks['callback'];

Or

List callbacks2 = [
{
  'name': "test",
  'callback': () => test(),
},

];

Vincent
  • 1
  • 1